简体   繁体   中英

Geneate Custom log Log4j.xml

I have simple program in java & want to generate the custom log using log4j.xml

I am getting the below output with the mentioned code at bottom.

2017-01-30 23:23:03 DEBUG Log4jXmlConfigurationExample:13 - Log4j appender configuration is successful !!
2017-01-30 23:23:03 INFO  Log4jXmlConfigurationExample:15 - Info message ---------- >
2017-01-30 23:23:03 ERROR Log4jXmlConfigurationExample:17 - Error message ------>

Here I getting 4 column like "DateTime" "LogType" "JavafileName" "Message"

Now I want the fifth column as userId. So I am expecting that output file will have 5 column as "DateTime" "LogType" "JavafileName" "Message" "Counter"

Could someone help here how can I make the custom changes in log4j.xml

Yes ofcourse counter will be dynamically generated and will get in simple java program

log4.xml

<appender name="file" class="org.apache.log4j.RollingFileAppender">
    <param name="append" value="false" />
    <param name="maxFileSize" value="10MB" />
    <param name="maxBackupIndex" value="10" />
    <param name="file" value="C:/Users/ADMIN/Desktop/Logs/my_logs.log" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern"
        value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>

<root>
    <level value="DEBUG" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
</root> 

Java Code

public static int counter =1;
public static void main(String[] args) {
    DOMConfigurator.configure("log4j-config.xml");
    counter++; 
    logger.debug("Log4j appender configuration is successful !!");
    counter++;      
    logger.info("Info message ---------- >");       
    counter++;
    logger.error("Error message ------>");
}

转换模式中列出了使用的变量(例如%d): https : //logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html我没有看到userID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM