3

This is my log4j.properties file.

log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#outputs to Tomcat home
log4j.appender.file.File=${catalina.home}/logs/myapp.log
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{1}:%L - %m%n

This is loading bunch of logs which are related to the framework etc.

I tried to put

log4j.rootLogger=DEBUG only to load only my logs. 

I want my log file to have only my project logs. Where should I need to change?

The way I'm logging my statements are

private Logger logger = Logger.getLogger(MyClass.class);

logger.debug("bla bla");
Aliy
  • 197
  • 14

2 Answers2

1

You should configure logger as per your application package name.

Suppose, your application package name is test.example then add below line in your log4j.properties -

log4j.logger.test.example = DEBUG, stdout, file
log4j.additivity.test.example = false

It will enable debug logs in classes which are present in test.example package as well as in sub packages.

For removing framework related logs, change rootLogger log level to ERROR or WARN -

log4j.rootLogger=ERROR, stdout, file

It will display only errors which are related to framework classes or classes which are not present in above package test.example and its subpackages.

Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
  • Vikas, this is not showing my own package logs too in console. – Aliy Sep 11 '17 at 04:51
  • @Aliy what about file. Messages are getting logged in file ? How you are logging messages in your class ? Can you post one sample class. – Vikas Sachdeva Sep 11 '17 at 05:11
  • Updated with logger statement too – Aliy Sep 11 '17 at 05:21
  • @Aliy where is log4j.properties file is placed ? is it on classpath ? In question you mentioned that you are getting framework related logs. Those logs statement are debug logs ? and you are getting them in console and in file ? – Vikas Sachdeva Sep 11 '17 at 06:00
  • properties file is directly placed under src. I'm getting logs of all packages inside it. I have some generated classes of web services, even those logs are getting printed both in console and file – Aliy Sep 11 '17 at 07:45
  • @Aliy I am not sure what exactly the problem is with provided information. What I told you is standard and proper way to configuring logs. You can check more details [here](https://stackoverflow.com/questions/11240418/log4j-properties-file-multiple-loggers-in-same-class) and [here](https://examples.javacodegeeks.com/enterprise-java/log4j/log4j-additivity-property-example) – Vikas Sachdeva Sep 11 '17 at 09:08
  • One point I would suggest to debug `log4j` using `log4j.debug` VM argument that would help to understand what is happening exactly – Vikas Sachdeva Sep 11 '17 at 09:11
-1
log4j.rootLogger=INFO,myproject

#My logger option
log4j.logger.com.myproject=ERROR
log4j.appender.myproject=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myprojectapp.File=${catalina.home}/logs/myproject.log
log4j.appender.myproject.DatePattern='.'yyyy-MM-dd
log4j.appender.myproject.layout=org.apache.log4j.PatternLayout
log4j.appender.myproject.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -%M - %m%n
log4j.appender.myproject.Threshold=DEBUG

#to not add myproject logs to other logger
log4j.additivity.com.myproject=false
Rahul
  • 128
  • 1
  • 14