0

Here is my log4j property file I am using console appender but i need to use file appender could anybody expline me how to write that and where to find that file after run my application....

 # Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.base}/logs/mylog.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
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
user2998826
  • 57
  • 1
  • 3
  • 8

2 Answers2

0

For appending to a file, follow the below properties

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
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

Visit this link for more details

to place in tomcat directory:

log4j.appender.file.File=${catalina.base}/logs/mylog.log
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
0

Here is the standard properties file which I use for my project.. this will print logging output on console as well as in a file.

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\springtest.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
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

# Direct log messages to stdout
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
Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
  • thats created now but i don't want to specify the drive i want store in project itself how to do that thanks – user2998826 – user2998826 Nov 21 '13 at 05:49
  • See this stackoverflow question where its been explained in detail.http://stackoverflow.com/questions/216781/log4j-configuring-a-web-app-to-use-a-relative-path – Bilbo Baggins Nov 21 '13 at 06:02
  • that is printing all server messages but not inside class messages...plz help me – user2998826 Nov 21 '13 at 07:24
  • Have you placed this in the class path ?? Does it shows some warning at the beginning of the stacktrace?? – Bilbo Baggins Nov 21 '13 at 09:45
  • now that works fine but how create superate class file for all the class – user2998826 Nov 21 '13 at 12:17
  • What you can do is write a separate class which will have static methods for logging information, debug-info and errors. Methods will have some implementation like this Logger.getLogger(MyClass.class).info("ANY INFO",OBJECT); this way you can keep your logging separate in a different class. – Bilbo Baggins Nov 22 '13 at 04:07