An old article I wrote: Practice log4j

Source: Internet
Author: User

Previous articles are stuck here for backup

Purpose:
1. Write a fatal-level error to the 2000nt log
2. Send email notification to administrators for warn, error, and fatal errors
3. Other errors are output directly in the background.

Tutorial steps:
Output to 2000nt log
1. Copy the nteventlogappender. dll in the log4j compressed package to the WINNT/system32 directory.
2. Write the configuration file log4j. properties.

# System log output in 2000
Log4j. Logger. ntlog = fatal, A8
# Appender A8
Log4j. appender. A8 = org. Apache. log4j. nt. nteventlogappender
Log4j. appender. a8.source = javatest
Log4j. appender. a8.layout = org. Apache. log4j. patternlayout
Log4j. appender. a8.layout. conversionpattern = %-4r %-5 p [% T] % 37C % 3x-% m % N

3. Call code:
Logger logger2 = logger. getlogger ("ntlog"); // The name must be the same as that set in the configuration file.
Logger2.debug ("Debug !!! ");
Logger2.info ("info !!! ");
Logger2.warn ("Warn !!! ");
Logger2.error ("error !!! ");
// Only this error will write the 2000 log
Logger2.fatal ("Fatal !!! ");

Send email notification to the Administrator:
1. First download javamail and JAF,
Http://java.sun.com/j2ee/ja/javamail/index.html
Http://java.sun.com/beans/glasgow/jaf.html
Reference mail. jar and activation. jar in the project.
2. Write the configuration file
# Send Logs to email
Log4j. Logger. maillog = warn, A5
# Appender A5
Log4j. appender. A5 = org.apache.log4j.net. smtpappender
Log4j. appender. a5.buffersize = 5
Log4j. appender. a5.to = chunjie@263.net
Log4j. appender. a5.from = error@error.com
Log4j. appender. a5.subject = errorlog
Log4j. appender. a5.smtphost = smtp.263.net
Log4j. appender. a5.layout = org. Apache. log4j. patternlayout
Log4j. appender. a5.layout. conversionpattern = %-4r %-5 p [% T] % 37C % 3x-% m % N
3. Call code:
// Send Logs to mail
Logger logger3 = logger. getlogger ("maillog ");
Logger3.warn ("Warn !!! ");
Logger3.error ("error !!! ");
Logger3.fatal ("Fatal !!! ");

Output errors of all types in the background:
1. Write the configuration file
# Output in the background
Log4j. Logger. Console = debug, A1
# Appender A1
Log4j. appender. A1 = org. Apache. log4j. leleappender
Log4j. appender. a1.layout = org. Apache. log4j. patternlayout
Log4j. appender. a1.layout. conversionpattern = %-4r %-5 p [% T] % 37C % 3x-% m % N
2. Call Code
Logger logger1 = logger. getlogger ("console ");
Logger1.debug ("Debug !!! ");
Logger1.info ("info !!! ");
Logger1.warn ("Warn !!! ");
Logger1.error ("error !!! ");
Logger1.fatal ("Fatal !!! ");

--------------------------------------------------------------------
All configuration files: log4j. Properties
# Output in the background
Log4j. Logger. Console = debug, A1
# Appender A1
Log4j. appender. A1 = org. Apache. log4j. leleappender
Log4j. appender. a1.layout = org. Apache. log4j. patternlayout
Log4j. appender. a1.layout. conversionpattern = %-4r %-5 p [% T] % 37C % 3x-% m % N

# System log output in 2000
Log4j. Logger. ntlog = fatal, A8
# Appender A8
Log4j. appender. A8 = org. Apache. log4j. nt. nteventlogappender
Log4j. appender. a8.source = javatest
Log4j. appender. a8.layout = org. Apache. log4j. patternlayout
Log4j. appender. a8.layout. conversionpattern = %-4r %-5 p [% T] % 37C % 3x-% m % N

# Send Logs to email
Log4j. Logger. maillog = warn, A5
# Appender A5
Log4j. appender. A5 = org.apache.log4j.net. smtpappender
Log4j. appender. a5.buffersize = 5
Log4j. appender. a5.to = chunjie@263.net
Log4j. appender. a5.from = error@error.com
Log4j. appender. a5.subject = errorlog
Log4j. appender. a5.smtphost = smtp.263.net
Log4j. appender. a5.layout = org. Apache. log4j. patternlayout
Log4j. appender. a5.layout. conversionpattern = %-4r %-5 p [% T] % 37C % 3x-% m % N

All code: log4jtest. Java
/*
* Creation date: 2003-11-13
*/
Package edu. BCU. Bean;
Import org. Apache. log4j .*;
// Import org. Apache. log4j. nt .*;
// Import org.apache.log4j.net .*;
/**
* @ Author yanxu
*/
Public class log4jtest
{
Public static void main (string ARGs [])
{
Propertyconfigurator. Configure ("log4j. properties ");
// Output in the background
Logger logger1 = logger. getlogger ("console ");
Logger1.debug ("Debug !!! ");
Logger1.info ("info !!! ");
Logger1.warn ("Warn !!! ");
Logger1.error ("error !!! ");
Logger1.fatal ("Fatal !!! ");

// Output logs in the NT System
Logger logger2 = logger. getlogger ("ntlog ");
// Nteventlogappender NLA = new nteventlogappender ();
Logger2.debug ("Debug !!! ");
Logger2.info ("info !!! ");
Logger2.warn ("Warn !!! ");
Logger2.error ("error !!! ");
// Only this error will write the 2000 log
Logger2.fatal ("Fatal !!! ");

// Send Logs to mail
Logger logger3 = logger. getlogger ("maillog ");
// Smtpappender SA = new smtpappender ();
Logger3.warn ("Warn !!! ");
Logger3.error ("error !!! ");
Logger3.fatal ("Fatal !!! ");
}
}

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.