pi-2r
pi-2r

Reputation: 1279

How to record log information from a specific class into a specific file?

currently, I wish to save all the log from a specify class in specific file. In my class, I've already instantiated my logger, and in my log4j.properties I do not know what information to write, in order to log all informations of this specific class in a specific file.

If someone has a example, I take it :)

Thanks in advance,

Upvotes: 0

Views: 1775

Answers (2)

Vishal Gajera
Vishal Gajera

Reputation: 4207

Hi, you can do by one of this way,

Method-1 :

First of all prepared 2-separate log4j configuration file let's say for example,

 1. log4JMy.properties ( for whole application , we can called it default)
 2. log4jMy2.properties ( for specific class here in your case )

into your specific class you can re-configure log4j properties likewise,

org.apache.log4j.PropertyConfigurator.configure("log4jMy2.properties");*

which will modify existence log4j configuration and you can override it by your log4jMy2.properties file.

so for that class works perfectly, however again you need to re-set to default so that again get back to log4j works normally/default manner.


Method-2 :

into your log4j.properties file,

assign different LEVEL, with suitable configuration changes, for-example,

log4j.rootLogger=DEBUG,FILE,ERROR_FILE

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.file=${catalina.base}/logs/myApplicationLogs.log
log4j.appender.FILE.Append=true
log4j.appender.FILE.Encoding=UTF-8
log4j.appender.FILE.MaxFileSize=5MB
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n

log4j.appender.ERROR_FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR_FILE.file=${catalina.base}/logs/error_myApplicationLogs.log
log4j.appender.ERROR_FILE.Append=true
log4j.appender.ERROR_FILE.Encoding=UTF-8
log4j.appender.ERROR_FILE.MaxFileSize=5MB
log4j.appender.ERROR_FILE.MaxBackupIndex=10
log4j.appender.ERROR_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ERROR_FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n

As per above configuration , while from application ,

LOG.debug("some text"); // it will print into myApplicationLogs.log file

wherever, if you are write,

LOG.error("some erroneous text "); // it will print into error_myApplicationLogs.log file.

it will print logs as per log4j configuration file-wise into your corresponds logging file, best of luck...!

Upvotes: 1

shad
shad

Reputation: 124

Here's a very basic example of a log4j.properties file that can get you started:

log4j.logger.com.yourpackage.yourclass=INFO, YourLoggerName

log4j.appender.YourLoggerName=org.apache.log4j.RollingFileAppender
log4j.appender.YourLoggerName.File=/path/to/logfile/forthisappender
log4j.appender.YourLoggerName.MaxFileSize=5M
log4j.appender.YourLoggerName.MaxBackupIndex=10
log4j.appender.YourLoggerName.layout=org.apache.log4j.PatternLayou
log4j.appender.YourLoggerName.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n

There's more complete info here

Upvotes: 2

Related Questions