简体   繁体   English

生成自定义日志Log4j.xml

[英]Geneate Custom log Log4j.xml

I have simple program in java & want to generate the custom log using log4j.xml 我在Java中有一个简单的程序,想使用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" 在这里,我得到了4列,例如“ DateTime”,“ LogType”,“ JavafileName”,“ Message”

Now I want the fifth column as userId. 现在,我希望第五列作为userId。 So I am expecting that output file will have 5 column as "DateTime" "LogType" "JavafileName" "Message" "Counter" 因此,我期望输出文件具有“ DateTime”,“ LogType”,“ JavafileName”,“ Message”,“ Counter”之类的5列。

Could someone help here how can I make the custom changes in log4j.xml 有人可以在这里帮助我如何在log4j.xml中进行自定义更改

Yes ofcourse counter will be dynamically generated and will get in simple java program 是的,当然计数器会动态生成,并会通过简单的Java程序获取

log4.xml 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 Java代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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