• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HOw to create user defined log file for an application

 
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all,
Any one tell me how to create user defined log file for an web application.

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you want to make log4j ??
 
Vt Guru
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.
Yes, I want to make log4j.
 
swati mittal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinoth Thirunavukarasu wrote:Thanks for your reply.
Yes, I want to make log4j.


Using Log4j :
Download the log4j software (http://logging.apache.org/log4j/1.2/download.html) and extract log4j.jar out of it. Include log4j.jar in your application's classpath so that logging methods can find the needed classes. Save the following sample code in a file named TestLogging.java somewhere in the classpath.
import org.apache.log4j.*;

// How to use log4j
public class TestLogging {

Static Logger logger=Logger.getLogger (TestLogging.class);
public static void main(String args []) {
// Try a few logging methods
logger.debug ("Start of main ()");
logger.info("Just testing a log message with priority set to INFO");
logger.warn("Just testing a log message with priority set to WARN");
logger.error("Just testing a log message with priority set to ERROR");
logger.fatal("Just testing a log message with priority set to FATAL");
}
}
Log4j by default can log messages with five priority levels.
1. Use debug to write debugging messages which should not be printed when the application is in production.
2. Use info for messages similar to the "verbose" mode of many applications.
3. Use warn for warning messages which are logged to some log but the application is able to carry on without a problem.
4. Use error for application error messages which are also logged to some log but, still, the application can hobble along. Such as when some administrator-supplied configuration parameter is incorrect and you fall back to using some hard-coded default value.
5. Use fatal for critical messages, after logging of which the application quits abnormally.
The default file which configures log4j is log4j.properties in the same directory as TestLogging.class.
Sample log4j.properties is :

# ROOT CATEGORY is used to set level of logger and appender name
log4j.rootCategory=DEBUG, CA, RA
# DEBUG is a root level and FA, CA, DA are appender name (appender name can be different)

# ---------- CA is set to be a ConsoleAppender ----------

# Write to Console(stdout or stderr).
log4j.appender.CA=org.apache.log4j.ConsoleAppender
# This appender will only log messages with priority equal to or higher than the one specified here
# hierarchy of level is (from lower to higher): DEBUG,INFO,WARN,ERROR,FATAL
log4j.appender.CA.Threshold=INFO
# appender layouts (log formats)
#log4j.appender.CA.layout=org.apache.log4j.SimpleLayout
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
# For a pattern layout, specify the pattern (Default is %m%n which is fastest)
log4j.appender.CA.layout.ConversionPattern=[%-5p] %c - %m%n

# ---------- RA is set to be a RollingFileAppender ----------

# Write log to a file, roll the file after some size
log4j.appender.RA=org.apache.log4j.RollingFileAppender
# This appender will only log messages with priority equal to or higher than the one specified here
log4j.appender.RA.Threshold=INFO
# The name of log file
log4j.appender.RA.File= C:/log4j.log
# The maximum log file size
log4j.appender.RA.MaxFileSize=100KB
# Don't append, overwrite
#log4j.appender.RA.Append=false
# Keep backup file(s) (backups will be in filename.1, .2 etc.)
log4j.appender.RA.MaxBackupIndex=1
# appender layouts (log formats)
#log4j.appender.RA.layout=org.apache.log4j.SimpleLayout
log4j.appender.RA.layout=org.apache.log4j.PatternLayout
# For a pattern layout, specify the pattern (Default is %m%n which is fastest)
log4j.appender.RA.layout.ConversionPattern=[%-5p] %c - %m%n

# ---------- DA is set to be a DailyRollingFileAppender ----------

# Write log to a file, roll the file every week
#log4j.appender.DA=org.apache.log4j.DailyRollingFileAppender
# This appender will only log messages with priority equal to or higher than the one specified here
#log4j.appender.DA.Threshold=ERROR
# The log file name
#log4j.appender.DA.File=C:/log4j.log
# Don't append, overwrite
#log4j.appender.DA.Append=false
# Rollover log file at the start of each week
#log4j.appender.DA.DatePattern='.'yyyy-ww
# appender layouts (log formats)
#log4j.appender.DA.layout=org.apache.log4j.SimpleLayout
#log4j.appender.DA.layout=org.apache.log4j.PatternLayout
# For a pattern layout, specify the pattern (Default is %m%n which is fastest)
#log4j.appender.DA.layout.ConversionPattern=[%-5p] %c - %m%n

ConsoleAppender will print logger at console. Here 2 more Appenders are used named RollingFileAppender, and DailyRollingFileAppender to print into file. At a time use one Appender according to the requirement. Here RollingFileAppender is used to print logger into a file at given path, here appender name RA is declared for RollingFileAppender. To use DailyRollingFileAppender declare the appender name in log4j.rootCategory DA instead of RA and open the comments of DailyRollingFileAppender.

 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on the above exchange, i understand that this question was about how to use log4j.
Moving to Other Open Source Products forum....
 
reply
    Bookmark Topic Watch Topic
  • New Topic