How to use log4j to output different levels, different package log to different files

Source: Internet
Author: User
Tags log4j
Log4j for me is always a Shing, basically is from other projects, or online to find a section down according to gourd painting scoop, anyway, are not insignificant things, never carefully studied.

Recently done a small project, the customer to look at the log than the business is important, have to improve their own, carefully studied a bit, finally from the muddle out of the.

This article does not intend to introduce the log4j in detail, so, such as log level (grade), Appender (output destination), Layout (output format) and print parameters, etc., no longer boil, because such articles on the Internet a sack, if you want to find this information, Then google it.

There are two key points to be described in this article:

1. How to use log4j to output different levels, different package log to different files

The relationship between the setting of 2.log4j and the division of package

This article will be from the actual combat point of view, the project on the log output requirements, and how to achieve such requirements, I hope that children's shoes useful.

I. Introduction to the project

Environment: Eclipse

Language: Java

Business: Divided into two modules of Web Department and batch System

Package:

├─x
│└─y
│└─z
│└─e

│└─w

The following is the Web Department │├─action
││└─interceptor
│├─dto
│├─entity
│├─exception
│├─hibernate
│├─listener
│├─service

│└─util

The following is Batch series │├─batch
││├─bean
││├─conf
││├─exception
││├─query

││└─util

Other └─template
└─simple


Ps:

It is easy to see from the above directory structure diagram, the Web Department and the batch system are not clearly separated, the source of batch system is all under "BACTH" package, but the source of the Web system does not define an independent package, but scattered in the "W" Package below.

This is a garbage design, due to my momentary negligence, not considerate of the cause. This will cause unnecessary trouble to the log4j configuration. Will explain its drawbacks and how to improve the problem later.

Two. Log Output requirements

1. Output only debug,info,error three levels of log

2. The Web Department log is exported to Web_debug.log, Web_info.log, web_error.log files, respectively.

3. The log of batch system is exported to Batch_debug.log, Batch_info.log, batch_error.log files, respectively.

PS: Note that the Web and batch systems are separate, that is to say, the log of the Web system cannot appear in the log of the batch system and not in the log of the batch system.

4. Each class,method's predetermined action will output the log specified in the design book.

Considering that the framework uses STRUTS2 + Hibernate, so struts and Hibernate output log, scheduled to close when the project is released.

However, to enable customers to easily put the two opensource log open.

However, in addition to other Third-party opensource output log, are not allowed to appear in the log file.

PS: Customer's understanding of log4j is 0 (he knows a little, but he commands me to look at him as a fool, which makes me very embarrassed, but I have to obey the order ^_^)

Other print formats and other information, omitted here, this is not the focus of this article

three. To achieve

[Plain]  View Plain Copy #--------------------------------------------------------------------      #log4j .properties     #-------------------------------------------------------------- ------          # log4j.rootlogger = arg1, arg2,  arg3,....      # arg1 =  ( debug | info | warn  | ERROR | ALL | OFF | FATAL )      # arg2 ,...  is appender.     #log4j. rootlogger = error, c0, a0,  A1, A2          #In  order to facilitate  all levels of the log output can be flexible,     # root logger is closed     log4j.rootlogger = off      &NBsp;    #------------------------     #Web  Log     # level :     #   followed from low to high is  :  ( debug | info | warn | error | all | off  | FATAL )      #   higher than the current  level log only will be output.     #appender  :      #   a0 : web debug log appender      #   a1 : web info log appender     #    a2 : web error lo

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.