log4j logger set default priority - java

I have an standalone java application with following config log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="1" />
<param name="File"
value=".\\myComp.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yy HH\:mm\:ss.SSS} %5p %c{1}:%L - %m%n " />
</layout>
</appender>
<category name="com.mycomp.project.starter">
<priority value="${project.client.log.level.starter}" />
</category>
<category name="com.security">
<priority value="${project.client.log.level.security}" />
</category>
<root>
<level value="ERROR" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
When I start the Application I can set the log level through a ini file which can contain:
-Dproject.client.log.level.starter=INFO
-Dproject.client.log.level.security=DEBUG
What I would like to archive is, if the -Dproject.client.log.level.security=DEBUG is not set it should use ERROR.
How could I achieve this? I appreciate any help.

Okay, I got a solution, unfortunately it involved switching to log4j2.
I found out that log4j allows Property Substitution allows to use system properties with prefix sys:. In combination with this finding (where I can set default values with a simple -), I changed my config to:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<RollingFile name="file" fileName=".\\myComp.log" append="true" filePattern=".\\myComp-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{dd.MM.yy HH:mm:ss.SSS} %5p %c{1}:%L - %m%n "/>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- setting log level per default to INFO if not set through system properties -->
<Logger name="com.myComp.starter" level="${sys:project.client.log.level.starter:-INFO}" />
<!-- setting log level per default to ERROR if not set through system properties -->
<Logger name="com.security" level="${sys:project.client.log.level.security:-ERROR}" />
<Root level="ERROR">
<AppenderRef ref="file"/>
</Root>
</Loggers>
</Configuration>
Now I am able to set it within a ini file or leave it out if not needed and still have a defined log level.

Related

log4j.xml configuration is not storing logs to file

I have the following log4j.xml configuration file, but don't see the log data stored in the file specified (in my documents folder: Documents/debug.log).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="true">
<appender name="applicationLogFile" class="org.apache.log4j.RollingFileAppender">
<param name="DatePattern" value="'.'yyyyMMdd'.log'" />
<param name="File" value="Documents/debug.log" />
<param name="threshold" value="TRACE" />
<param name="MaxNumberOfDays" value="90"/>
<param name="CompressBackups" value="TRUE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c] %m%n" />
</layout>
</appender>
<logger name="APP_VERSION_LOGGER">
<appender-ref ref="applicationVersionFile" />
</logger>
<logger name="org.springframework">
<level value="WARN" />
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="applicationLogFile" />
<appender-ref ref="bmcAlertFile" />
</root>
</log4j:configuration>
How an I get my log data sent to my Documents directory on my computer? I'm using log4j-core version 2.13.3. And yes, I've looke at the docs, but I still don't see where my log data is going.
Your line here says:
<param name="File" value="Documents/debug.log" />
It will make a folder with name Documents on your project root level and logs will be there.
I f you want to put them in to specific location on your machine then you need to provide full pathname.
ex.
<param name="File" value="/Users/myUser/Documents/debug.log" />

Jetty: Debug logging for specific Java class not working

I am working on a Spring-MVC application in which I am using Jetty as our application server. I want to debug log a specific java class, but even after enabling log4j or slf4j, and adding files it's not working.
Finally, I created a war file, added to start log4j with this command java -jar start.jar --add-to-start=logging-log4j, which created a log4j.xml file in resources directory. In that log4j.xml, I added the class I want to debug, but no [DEBUG] entries are getting added.
Enabled modules :
log4j2-slf4j.mod
slf4j-log4j2.mod
slf4j-api.mod
logging-slf4j.mod
I have also tried their combinations and different loggings as documentation suggested.
log4j.xml present in resources directory of both IDE and Jetty.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<!-- console appender -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender>
<logger name="com.zaxxer.hikari" additivity="false">
<level value="DEBUG" />
<appender-ref ref="console" />
</logger>
<root>
<priority value="ERROR" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
What am I doing wrong?
It seems that the problem is that you have only bridges and not log4j itself in the classpath.
As requested a simple logback.xml config file:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="test" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

How to create different files for logger hierarchy in tomcat?

In my Tomcat application, I want to use two loggers to log general event informations and errors to two different files.
Logger eventLogger = Logger.getLogger("event");
Logger errorLogger = Logger.getLogger("error");
Now I want to do the following: the logs of the eventLogger should be written to a "events.log" file, and the errorLogger logs should be written in a "errors.log" file. All logs from any other logger (e.g. Tomcat logs) should be written to catalina.out (or any other default file).
How can I achive this with JULI? Or do I need a third party lib?
This can't be achieved with Juli. You might use log4j.
Basically you need appenders. They will manage the logs to go to separate files. Example configuration in xml:
<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.EnhancedPatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%t][%c{1}:%L] %m%n" />
</layout>
</appender>
<appender name="eventFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="#tomcat.home#/logs/event.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="errorFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="#tomcat.home#/logs/error.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<logger name="event" additivity="false">
<level value="INFO" />
<appender-ref ref="eventFILE" />
</logger>
<logger name="error" additivity="false">
<level value="INFO" />
<appender-ref ref="errorFILE" />
</logger>
<root>
<priority value="INFO" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
you have to define an appender for each file in your log4j.xml and connect them to your loggers:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p [%c.%M:%L] - %m%n" />
</layout>
</appender>
<appender name="FILE-error" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.base}/logs/error.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="ImmediateFlush" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p [%c.%M:%L] - %m%n" />
</layout>
</appender>
<appender name="FILE-event" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.base}/logs/event.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="ImmediateFlush" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss};%m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="error" additivity="false">
<level value="warn" />
<appender-ref ref="FILE-error" />
</logger>
<logger name="event" additivity="false">
<level value="info" />
<appender-ref ref="FILE-event" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="FILE-error" />
</root>
</log4j:configuration>
Tomcat documentation does not provide any way to define more than one file per application when using JULI. Actually it provides the steps to use log4j instead of it because JULI configuration can be too basic.
But again this configuration is at container level:
Note: The steps described in this section are needed when you want to
reconfigure Tomcat to use Apache log4j for its own logging. These
steps are not needed if you just want to use log4j in your own web
application. — In that case, just put log4j.jar and log4j.properties
into WEB-INF/lib and WEB-INF/classes of your web application.
So using a third party library is the logical choice and you can find how to configure it in this SO post
Tomcat JULI logging supports this only via the logging.properties located in the ${catalina.base}/config folder.
#Declares the handlers allowed for use.
handlers = 100catalina.org.apache.juli.FileHandler, 200catalina.org.apache.juli.FileHandler, 300catalina.org.apache.juli.FileHandler
#Install the 'all' hander on the root logger.
.handlers=300catalina.org.apache.juli.FileHandler
#Install the 'event' handler on the 'event' logger and don't report to root.
event.handlers=100catalina.org.apache.juli.FileHandler
event.useParentHandlers=false
#Install the 'error' handler on the 'error' logger and don't report to root.
error.handlers=200catalina.org.apache.juli.FileHandler
error.useParentHandlers=false
#Event handler settings.
100catalina.org.apache.juli.FileHandler.level = ALL
100catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
100catalina.org.apache.juli.FileHandler.prefix = event.
#Error handler settings.
200catalina.org.apache.juli.FileHandler.level = ALL
200catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
200catalina.org.apache.juli.FileHandler.prefix = error.
#Root handler settings.
300catalina.org.apache.juli.FileHandler.level = ALL
300catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
300catalina.org.apache.juli.FileHandler.prefix = all.
This config will not work if you try to install this file in the WEB-INF/classes/logging.properties. This is because the ClassLoaderLogManager doesn't allow you to install handlers on named loggers.

Log4j, disable appenders dependent on environment

I would like to have one log4j.xml config and be able to log to console while developing my application. Once deployed to an environment I want to only log to a file appender and not the console. How can I achieve this?
This is my current config:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${log-base-dir}/${adapter-name}.log" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="99" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="info" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4j:configuration>
One of the solutions could be separation of log4j configuration files for development and production environments e.g.:
log4j-development.xml - for development environment
log4j-production.xml - for production environment
Then your application startup command could have parrameter specifying log4j configuration file e.g. java -Dlog4jconfig=log4j-development.xml -jar Application.jar
You can configure log4j by getting value of log4jconfig property in your code e.g. System.getProperty("log4jconfig").
Pros of that solution are as follows:
You can specify loggers independently (ConsoleAppender and LogFileAppender in development and only LogFileAppender in production)
You can specify your logging level per environment (e.g. error in production and debug in development)
You can configure file logger independently e.g. keep logs for X days in production (for audit purposes etc.) and have only one log file in development etc.
That pattern is used in many application servers where you have multiple environments (Development, UAT, Staging, Production etc.)
Example of log4j-development.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4j:configuration>
Example of log4j-production.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="LogFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${log-base-dir}/${adapter-name}.log" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="99" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="error" />
<appender-ref ref="LogFileAppender" />
</root>
</log4j:configuration>
It is easy to do using appender's Threshold parameter and JVM system property. E.g.
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="${my.console.level}" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n" />
</layout>
</appender>
Then when starting application on prod use -Dmy.console.level=OFF
At the same time when starting locally use -Dmy.console.level=ALL
Both OFF and ALL are valid log4j levels.
With Log4j2 you could alternatively switch the appender reference with a Java system property like so:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
<Appenders>
<!-- Log file location uses Tomcat system variable, change for other web servers -->
<RollingFile name="rolling-file"/>
<Console name="console"/>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="${sys:log4j.loggers.root.appender:-rolling-file}"/>
</Root>
</Loggers>
</Configuration>
(the minus - in front of the default value in the sys prop variable is required for some reason)
To switch from the default "rolling-file" to the console"
-Dlog4j.loggers.root.appender=console

“log4j:configuration” must match “(renderer….loggerFactory)?)”

getting error, when deploying my war file to tomcat:
log4j:WARN The content of element type "log4j:configuration" must match "(renderer*,throwableRenderer?,appender*,plugin*,(category|logger)*,root?,(categoryFactory|loggerFactory)?)"
I googled around and found out that ordering of my log4j.xml could be wrong, but it should be correct.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d %c %x - %m%n"/>
</layout>
</appender>
<root>
<priority value ="error" />
<appender-ref ref="console" />
</root>
<category name="org.springframework" additivity="false">
<priority value="info" />
<appender-ref ref="console" />
</category>
</log4j:configuration>
Any suggestions?
According to the error message, the DTD expects all category elements to be ahead of the root element. You have them the wrong way round.
Follow this order:
<renderer></renderer>
<appender></appender>
<plugin></plugin>
<logger></logger>
<category></category>
<root></root>
<loggerfactory></loggerfactory>
<categoryfactory></categoryfactory>

Categories

Resources