I configured the logging in my Play Framework application. When I run application in prod or dev mode or run test by comand (play test) - everything works fine, but test can not executes when I run their 'play auto-test'. Please help!
in application.conf:
application.log=INFO
application.log.path=/log4j.xml
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="File" value="${application.path}/logs/application.log"/>
<param name="MaxFileSize" value="1MB"/>
<param name="MaxBackupIndex" value="100"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p ~ %m %n"/>
</layout>
</appender>
<logger name="play">
<level value="error"/>
</logger>
<root>
<priority value="error"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
When I use the following log4j.xml file:
<?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="%m%n"/>
</layout>
</appender>
<logger name="play">
<level value="debug"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
tests running the command 'play auto-test' successfully executed. Please tell how configure logging in Play (output to file) that run and executes tests in 'play auto-test'!
Thi is pretty much a Play! bug. It's not caused by the fact that you've configured log4j with a separate file, though doing that will hide another error message: "play.tmp is null when it should be play.tmp=none" (something to this liking - you'll get this if you remove your custon log4j.xml file and re-do "play auto-test"). Problem is that even if you set that to play.tmp=none it will still not work. Besides, if you compare their online documentation to Play!'s console output you'll already realize that something's wrong:
The docs say:
"The 'auto-test' command do the same than the 'test' command, but it automatically launch a browser, run all the tests, and stop."
Console sais (when you do "play auto-test"):
ATTENTION: You're running Play! in DEV mode
~
~ Go to http://localhost:9000/#tests to run the tests
~
The answer was very simple
in application.conf:
%test.application.log=INFO
%test.application.log.path=/log4j.xml
application.log=INFO
application.log.path=/log4j.properties
application.log.system.out=off
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="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<logger name="play">
<level value="debug"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
log4j.properties:
log4j.rootLogger=ERROR, Rolling
log4j.logger.play=INFO
log4j.appender.Rolling=org.apache.log4j.RollingFileAppender
log4j.appender.Rolling.File=${application.path}/logs/application.log
log4j.appender.Rolling.MaxFileSize=1MB
log4j.appender.Rolling.MaxBackupIndex=100
log4j.appender.Rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.Rolling.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m %n
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n
Related
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>
I've tried I think all the info I've found in the Internet, with no results. I want to switch off the Hibernate Logging.
My log4j.properties:
log4j.logger.org.hibernate = INFO
log4j.logger.org.hibernate.SQL=INFO, SQL_APPENDER
log4j.additivity.org.hibernate.SQL=false
log4j.rootLogger=INFO
My log4.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<logger name="org.hibernate">
<level value="info"/>
</logger>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false"/>
<param name="file" value="out/learning.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>
Inside my hibernate.cfg.xml:
<property name="hibernate.show_sql">false</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.use_sql_comments">false</property>
At this moment I don't know what else can I do to stop those disturbing messages on my console.
Thanks in advance for your help.
try to use OFF level for each appender might help
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
I would encourage you to read the SLF4J Manual, you should only have 1 Binding jar on your classpath.
Remove slf4-simple-1.4.2.jar from your classpath, it may be stopping log4j being used.
slf4j-simple-1.7.5.jar
Binding for Simple implementation, which outputs all events to System.err. Only messages of level INFO and
higher are printed. This binding may be useful in the context of small
applications.
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
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>
Merged with Log4j not finding custom appender using a property file.
I'm working on an Eclipse RCP project comprised from multiple plugin projects, and having trouble load a custom appender using the following property file in log4j:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="eclipseErrorView" class="com.mypackage.EclipseLoggingAppender">
</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>
</appender>
<root>
<priority value ="warn" />
<appender-ref ref="eclipseErrorView" />
</root>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
But when coming to load this property file I get the following error:
log4j:ERROR Could not create an Appender. Reported error follows.
java.lang.ClassNotFoundException: com.mypackage.EclipseLoggingAppender
Where should I include this package in order for log4j to recoginze it? Note that I'm working on multiple plugin projects, all of them use log4j for logging purposes...
Thanks