How can I create multiple log files and save them on a local folder?
I need to create one file for each level(ERROR, INFO, etc.). How do I configure the logger to do this? Thanks.
Hi phneville,
Please follow these steps:
1. In CRXDE, create a new folder named "config" inside /apps/<%YOUR_APP%>.
2. Inside config, create a new node with:
Name: org.apache.sling.commons.log.LogManager.factory.config-demo
Type : sling:OsgiConfig (select from drop down)
3. Click Finish to save changes.
4. In CRXDE, select the node apps/<%YOUR_APP%>/config/ org.apache.sling.commons.log.LogManager.factory.config-demo and add the following properties.
Property Name Type Value
org.apache.sling.commons.log.file String ../logs/demo.log
org.apache.sling.commons.log.level String Debug
org.apache.sling.commons.log.names String apps.<%YOUR_APP%>
org.apache.sling.commons.log.pattern String {0,date,dd.MM.yyyy HH:mm:ss.SSS} *{4}* [{2}] {3} {5}
5. Add the log commands to the component file
Eg: log.debug(“inside filename.jsp”);
6. Run the component.
7. Navigate to "<%cq-install-directory%>/crx-quickstart/logs‟.
8. Open the demo.log file to see the log statements
And you should be good to go...
hi anuumar, I created a new project to test the logging to multiple log files. I created multiple configuration to generate 3 log files base on the level(PR-Error.log, PR-Debug.log, PR-Info.log). The problem is only the PR-Debug.log is being written with log information.. the two other files is empty.
Here is my current configuration:
Under the project folder I have "config" node of type sling:Folder.
Under the config node I have the following:
org.apache.sling.commons.log.LogManager.factory.config-debug
jcr:primaryType sling:OsgiConfig
org.apache.sling.commons.log.file String ../logs/PR-Debug.log
org.apache.sling.commons.log.level String debug
org.apache.sling.commons.log.names String[] apps.SampleLogging#com.sample.logging
org.apache.sling.commons.log.pattern String {0,date,dd.MM.yyyy HH:mm:ss.SSS} *{4}* [{2}] {3} {5}
org.apache.sling.commons.log.LogManager.factory.config-error
jcr:primaryType sling:OsgiConfig
org.apache.sling.commons.log.file String ../logs/PR-Error.log
org.apache.sling.commons.log.level String debug
org.apache.sling.commons.log.names String[] apps.SampleLogging#com.sample.logging
org.apache.sling.commons.log.pattern String {0,date,dd.MM.yyyy HH:mm:ss.SSS} *{4}* [{2}] {3} {5}
org.apache.sling.commons.log.LogManager.factory.config-info
jcr:primaryType sling:OsgiConfig
org.apache.sling.commons.log.file String ../logs/PR-Info.log
org.apache.sling.commons.log.level String debug
org.apache.sling.commons.log.names String[] apps.SampleLogging#com.sample.logging
org.apache.sling.commons.log.pattern String {0,date,dd.MM.yyyy HH:mm:ss.SSS} *{4}* [{2}] {3} {5}
Then I also created writers for each log file:
org.apache.sling.commons.log.LogManager.factory.writer-debug
jcr:primaryType sling:OsgiConfig
org.apache.sling.commons.log.file String ../logs/PR-Debug.log
org.apache.sling.commons.log.file.size String '.'yyyy-MM-dd
org.apache.sling.commons.log.file.number Long 5
org.apache.sling.commons.log.LogManager.factory.writer-error
jcr:primaryType sling:OsgiConfig
org.apache.sling.commons.log.file String ../logs/PR-Error.log
org.apache.sling.commons.log.file.size String '.'yyyy-MM-dd
org.apache.sling.commons.log.file.number Long
org.apache.sling.commons.log.LogManager.factory.writer-info
jcr:primaryType sling:OsgiConfig
org.apache.sling.commons.log.file String ../logs/PR-Info.log
org.apache.sling.commons.log.file.size String '.'yyyy-MM-dd
org.apache.sling.commons.log.file.number Long
In the java file I have the following:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger LOGGER = LoggerFactory.getLogger(SampleUtil.class);
then I use the the LOGGER like so:
LOGGER.debug("Debug log.");
LOGGER.info("Info log.");
LOGGER.error("Error log.");
Thanks
North America
Europe, Middle East and Africa
Asia Pacific