Log4j2 RollingFile Appender - add custom info at the start of each logfile

前端 未结 3 1149
野的像风
野的像风 2020-12-10 22:49

Using log4j2 (beta9) with java 1.7.

My complete log4j2.xml:



  

        
3条回答
  •  [愿得一人]
    2020-12-10 23:39

    New solution available

    Some time has passed since this question was asked, and now, when I wanted to do the same I have found out that it is possible to solve without messing with factories, but it was not easy to find. The log4j2 team have made it possible to do this by configuration. I hope my post will be of use and save time for someone else.

    They have hidden this feature in the PatternLayout element. Not where I first looked, but why complain when it works?

    This is my configuration (pay attention to header and footer, and the properties they use):

    
    
        
            ${sys:catalina.home}/logs/my-app
            ${log-path}/archive
            [%d] [%-5p] [%t] %C{5} - %m%n
            450
            [%d] Start of log \n========================================================================\n
    Will be archived in ${archive}\n\n
            \n========================================================================\n[%d] End of log
        
        
            
                
                
            
    
            
                
                
                    
                    
                    
                
    
                
                
            
    
            
                
                
                    
                    
                    
                
                
                
            
    
            
                
                
                    
                    
                    
                
                
                
            
        
    
        
            
                
                
                
            
    
            
                
                
                
            
    
            
                
                
                
            
    
            
                
                
                
            
    
            
            
                
                
                
                
            
        
    
    

    As you can see I have included a timestamp, and a property, that includes a system property. Log4j2 can show many different kinds of properties, with this you can do a lot of things of what you asked for.

    The log file looks like this:

    [2016-08-09 17:00:43,924] Start of log 
    ========================================================================
    
    Will be archived in /home/emanciperingsivraren/program/apache-tomcat-8.0.32/logs/my-app/archive
    
    [2016-08-09 17:00:44,000] [INFO ] [RMI TCP Connection(2)-127.0.0.1]    [snip]
    
    ========================================================================
    [2016-08-09 17:02:17,871] End of log
    

    Do you need more custom information? - try to put that information in properties, system properties, or something else that log4j2 can read.

    See Property Substitution in log4j2 for details in what kind of properties you can have.

    Comments about the configuration

    • I use properties instead of repeating common settings
    • If something is written in the root logger, then it is unhandled, and a logger should be written to define what we want to do with it
    • Since errors/exceptions can be "hidden" among a lot of other log messages they are also printed there, and can easily be found
    • Messages printed on Console have different colours

提交回复
热议问题