JAVA log4j Log File Usage, javalog4j Log File

Source: Internet
Author: User

JAVA log4j Log File Usage, javalog4j Log File

// 1. Import jar package, log4j-1.2.17.jar
// 2. Create the log4j. properties file under src
// 3. Configure the properties File
/*
Log4j. rootLogger (effective for the entire project by default) = DEBUG, stdout (console), a, B ,...
Log Level (ALL <DEBUG <INFO <WARN <ERROR <FATAL <OFF)
Log4j.logger.com. hutu = info, stdout, indicating that the log is only valid for package com.

Log4j. appender. a = org. apache. log4j. Where is the log output?
ConsoleAppender (console)
FileAppender (file)
DailyRollingFileAppender (a log file is generated every day)
RollingFileAppender (a new file is generated when the file size reaches the specified size)
WriteAppender (send log information to any specified place in stream format)
JDBCAppender (save log information to the database)
Log4j. appender. a. File = File directory and File path $ {user. home}/logs /...
Log4j. appender. a. MaxFileSize = maximum file size 100 MB
Log4j. appender. a. MaxBackupIndex = 10 backup files
Log4j. appender. ServerDailyRollingFile. DatePattern = '. 'yyyy-MM-dd log suffix format
Log4j. appender. a. layout = org. apache. log4j. Log layout format
HTMLLayout (HTML table layout)
SimpleLayout (Level and information string containing log information)
TTCCLayout (including the log generation time, execution thread, category, and other information)
PatternLayout (you can flexibly specify the layout format, which is commonly used)
Log4j. appender. a. layout. ConversionPattern = % d {yyyy-MM-dd HH: mm: ss} % p [% c] % m % n log output format
Log4j. appender. ServerDailyRollingFile. Append = true

# The Log4j configuration file can be directly copied from the output file to the console, file, rollback file, log mail, database log table, and custom tag.
Log4j. rootLogger = DEBUG, console, logFile, rollingFile, dailyFile, socket .......
Log4j.additivity.org. apache = true


# Console)
Log4j. appender. console = org. apache. log4j. leleappender
Log4j. appender. console. Threshold = DEBUG
Log4j. appender. console. ImmediateFlush = true
Log4j. appender. console. Target = System. err
Log4j. appender. console. layout = org. apache. log4j. PatternLayout
Log4j. appender. console. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n

# LogFile)
Log4j. appender. logFile = org. apache. log4j. FileAppender
Log4j. appender. logFile. Threshold = DEBUG
Log4j. appender. logFile. ImmediateFlush = true
Log4j. appender. logFile. Append = true
Log4j. appender. logFile. File = D:/logs/log. log4j
Log4j. appender. logFile. layout = org. apache. log4j. PatternLayout
Log4j. appender. logFile. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n

# RollingFile)
Log4j. appender. rollingFile = org. apache. log4j. RollingFileAppender
Log4j. appender. rollingFile. Threshold = DEBUG
Log4j. appender. rollingFile. ImmediateFlush = true
Log4j. appender. rollingFile. Append = true
Log4j. appender. rollingFile. File = D:/logs/log. log4j
Log4j. appender. rollingFile. MaxFileSize = 200KB
Log4j. appender. rollingFile. MaxBackupIndex = 50
Log4j. appender. rollingFile. layout = org. apache. log4j. PatternLayout
Log4j. appender. rollingFile. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n

# Regularly roll back log files (dailyFile)
Log4j. appender. dailyFile = org. apache. log4j. DailyRollingFileAppender
Log4j. appender. dailyFile. Threshold = DEBUG
Log4j. appender. dailyFile. ImmediateFlush = true
Log4j. appender. dailyFile. Append = true
Log4j. appender. dailyFile. File = D:/logs/log. log4j
Log4j. appender. dailyFile. DatePattern = '. 'yyyy-MM-dd
Log4j. appender. dailyFile. layout = org. apache. log4j. PatternLayout
Log4j. appender. dailyFile. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n

# Apply to socket
Log4j. appender. socket = org. apache. log4j. RollingFileAppender
Log4j. appender. socket. RemoteHost = localhost
Log4j. appender. socket. Port = 5001
Log4j. appender. socket. LocationInfo = true
# Set up for Log Factor 5
Log4j. appender. socket. layout = org. apache. log4j. PatternLayout
Log4j. appender. socket. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n
# Log Factor 5 Appender
Log4j. appender. LF5_APPENDER = org. apache. log4j. lf5.LF5Appender
Log4j. appender. LF5_APPENDER.MaxNumberOfRecords = 2000

# Send Logs to a specified email
Log4j. appender. mail = org.apache.log4j.net. SMTPAppender
Log4j. appender. mail. Threshold = FATAL
Log4j. appender. mail. BufferSize = 10
Log4j. appender. mail. From = xxx@mail.com
Log4j. appender. mail. SMTPHost = mail.com
Log4j. appender. mail. Subject = Log4J Message
Log4j. appender. mail. To = xxx@mail.com
Log4j. appender. mail. layout = org. apache. log4j. PatternLayout
Log4j. appender. mail. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n

# Apply to databases
Log4j. appender. database = org. apache. log4j. jdbc. JDBCAppender
Log4j. appender. database. URL = jdbc: mysql: // localhost: 3306/test
Log4j. appender. database. driver = com. mysql. jdbc. Driver
Log4j. appender. database. user = root
Log4j. appender. database. password =
Log4j. appender. database. SQL = INSERT INTO LOG4J (Message) VALUES ('= [%-5 p] % d (% r) --> [% t] % l: % m % x % n ')
Log4j. appender. database. layout = org. apache. log4j. PatternLayout
Log4j. appender. database. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n

# Custom Appender
Log4j. appender. im = net. cybercorlin. util. logger. appender. IMAppender
Log4j. appender. im. host = mail.cybercorlin.net
Log4j. appender. im. username = username
Log4j. appender. im. password = password
Log4j. appender. im. recipient = Carolina@cybercorlin.net
Log4j. appender. im. layout = org. apache. log4j. PatternLayout
Log4j. appender. im. layout. ConversionPattern = [%-5 p] % d (% r) --> [% t] % l: % m % x % n

// 4. java class
Private static Logger logger = Logger. getLogger (Log. class. getName ());
PropertyConfigurator. configure ("src // log4j. properties"); // You can omit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.