log4j configuration via JVM argument(s)?

Do you have a log4j configuration file ? Just reference it using -Dlog4j.configuration={path to file} where {path to file} should be prefixed with file: Edit: If you are working with log4j2, you need to use -Dlog4j.configurationFile={path to file} Taken from answer https://stackoverflow.com/a/34001970/552525

Read a file line by line in reverse order

[EDIT] By request, I am prepending this answer with the sentiment of a later comment: If you need this behavior frequently, a “more appropriate” solution is probably to move your logs from text files to database tables with DBAppender (part of log4j 2). Then you could simply query for latest entries. [/EDIT] I would probably … Read more

Dynamically Changing log4j log level

Changing the log level is simple; modifying other portions of the configuration will pose a more in depth approach. LogManager.getRootLogger().setLevel(Level.DEBUG); The changes are permanent through the life cyle of the Logger. On reinitialization the configuration will be read and used as setting the level at runtime does not persist the level change. UPDATE: If you … Read more

In log4j, does checking isDebugEnabled before logging improve performance?

In this particular case, Option 1 is better. The guard statement (checking isDebugEnabled()) is there to prevent potentially expensive computation of the log message when it involves invocation of the toString() methods of various objects and concatenating the results. In the given example, the log message is a constant string, so letting the logger discard … Read more

log4j: How to use SocketAppender?

You can run the server using java -classpath log4j.jar org.apache.log4j.net.SimpleSocketServer 4712 log4j-server.properties The SimpleSocketServer receives logging events sent to the specified port number by the remote SocketAppender, and logs them as if they were generated locally, according to the configuration you supply in log4j-server.properties. It’s up to you to configure the relevant console/file/rolling file appenders … Read more

Configuring Hibernate logging using Log4j XML config file?

From http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-logging Here’s the list of logger categories: Category Function org.hibernate.SQL Log all SQL DML statements as they are executed org.hibernate.type Log all JDBC parameters org.hibernate.tool.hbm2ddl Log all SQL DDL statements as they are executed org.hibernate.pretty Log the state of all entities (max 20 entities) associated with the session at flush time org.hibernate.cache Log all … Read more

log4j: Log output of a specific class to a specific appender

An example: log4j.rootLogger=ERROR, logfile log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender log4j.appender.logfile.datePattern=’-‘dd’.log’ log4j.appender.logfile.File=log/radius-prod.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x – %m\n log4j.logger.foo.bar.Baz=DEBUG, myappender log4j.additivity.foo.bar.Baz=false log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender log4j.appender.myappender.datePattern=’-‘dd’.log’ log4j.appender.myappender.File=log/access-ext-dmz-prod.log log4j.appender.myappender.layout=org.apache.log4j.PatternLayout log4j.appender.myappender.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x – %m\n

How can I create 2 separate log files with one log4j config file?

Try the following configuration: log4j.rootLogger=TRACE, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] – %m%n log4j.appender.debugLog=org.apache.log4j.FileAppender log4j.appender.debugLog.File=logs/debug.log log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] – %m%n log4j.appender.reportsLog=org.apache.log4j.FileAppender log4j.appender.reportsLog.File=logs/reports.log log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] – %m%n log4j.category.debugLogger=TRACE, debugLog log4j.additivity.debugLogger=false log4j.category.reportsLogger=DEBUG, reportsLog log4j.additivity.reportsLogger=false Then configure the loggers in the Java code accordingly: static final Logger debugLog = Logger.getLogger(“debugLogger”); static final Logger resultLog = Logger.getLogger(“reportsLogger”); Do you … Read more