Java log4j Configure multiple logger to write multiple log files

Source: Internet
Author: User
Tags log log log4j

The version used here is for Log4j-1.2.15,jar resources to get the download. first, the introduction

During the normal development process, sometimes do not want to print all the logs to the same log file, in addition to the Java Engineering Integration log4j 1.x posting introduction of the use of a logger, Configuring multiple Appender Log4j.appender.d.threshold properties, you can also configure multiple logger in the configuration file log4j.properties. second, the specific configuration

In this configuration, in addition to the default Rootlogger, add a secondlogger, please note the specific configuration syntax.

log4j.rootlogger =debug,console,file Log4j.logger.secondLogger, Secondadpter Log4j.appender.console=org.apache.log4j.consoleappender log4j.appender.console.layout= Org.apache.log4j.PatternLayout log4j.appender.console.layout.conversionpattern=[%5p]%d{yyyy-mm-dd HH:mm:ss} [%t] ( %f:%l)%m%n Log4j.appender.file=org.apache.log4j.dailyrollingfileappender log4j.appender.file.file=./log/ First.log log4j.appender.file.datepattern= '. '
YYYY-MM-DD Log4j.appender.file.layout=org.apache.log4j.patternlayout

LOG4J.APPENDER.FILE.LAYOUT.CONVERSIONPATTERN=[%5P]%d{yyyy-mm-dd HH:mm:ss} [%t] (%f:%l)%m%n Log4j.appender.secondadpter=org.apache.log4j.dailyrollingfileappender log4j.appender.secondadpter.file=./log/ Second.log log4j.appender.secondadpter.datepattern= '. '
YYYY-MM-DD Log4j.appender.secondadpter.layout=org.apache.log4j.patternlayout LOG4J.APPENDER.SECONDADPTER.LAYOUT.CONVERSIONPATTERN=[%5P]%d{yyyy-mm-dd HH:mm:ss} [%t] (%f:%l)%m%n 
iii. use of Secondlogger

You can write the log separately to the file by using the following methods to get Secondlogger./log/second.log:

Private static final Logger log = Logger.getlogger ("Secondlogger");

The Logger class introduced here is import Org.apache.log4j.Logger; Iv. Error Usage

It should be explained that the following classes are introduced:

Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;

Writing to the./log/second.log log separately using the following Java code is a failure.

private static Final log = Logfactory.getlog (Secondlogger.class);

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.