Java – log4j2 why log4j?
I must miss something, but now I've been watching it for a few days, but why can you use log4j2 (not performance) before log4j?
From what I've seen so far, log4j2 has been published as a simpler configuration, but it's actually more complex (it's been three days now, and I still can't write logs in the home directory) Automatic configuration doesn't work for me at all (or at least I can't work). The configuration file itself is significantly more complex in its structure, and it seems more difficult to add something at run time to help diagnose
So apart from performance, is there any reason to use log4j2 better than the original log4j?
Solution
From log4j 1 X reasons for upgrading to log4j 2
>Community support: log4j 1 X is not actively maintained, but log4j 2 has an active community. Questions are answered, functions are added, and errors are fixed Update: since August 2015, log4j 1 X is officially end of life. It is recommended to upgrade to log4j 2 Update 2: log4j 1.2 is broken in Java 9. > Async loggers – performance similar to logging switched off > custom log levels > automatically reload their configuration after modification without losing log events during reconfiguration. > Java 8 style lambda support is used to delay logging > from version 2.6, log4j 2 is garbage free (or at least low garbage) > filtering: filtering based on context data, tags, regular expressions and other components in log events Filters can be associated with recorders You can use the commonly used filter class in any of these situations. > Plugin architecture – the API supported by > can be easily extended by building custom components: slf4j, common logging, log4j-1 X and Java util. The logging > log4j 2 API is separate from the log4j 2 implementation The API supports more than just record strings: charsequences, objects, and custom messages Messages allow interesting and complex structures to be supported for delivery through the logging system and to be manipulated effectively Users are free to create their own message types and write custom layouts, filters and lookups to manipulate them. > Concurrency improvement: log4j2 uses Java util. The concurrent library performs locking at the lowest level Log4j-1. X known deadlock problem. > Configure through XML, JSON, yaml, attribute configuration file or programmatically
awake to
> log4j2. XML and log4j2 Properties format is different from log4j 1.2 configuration syntax > log4j 2 is not fully log4j 1 X compatibility: the log4j 1.2 API is supported by the log4j-1.2-api adapter, but custom devices that rely on the internal mechanism of log4j 1.2 may not work properly. > Java 6.0 is required for versions 2.0 to 2.3 Log4j 2.4 and later require Java 7.0
Tips for upgrading
Common problems people encounter when they start using log4j2:
>You need to (at least) log 4j-api-2.6. In your classpath 2. Jar and log4j-core-2.6 2. Jar > log4j2 find a log4j2 XML configuration file instead of a log4j XML configuration file > configuration file location: put it in the classpath or use log4j The configurationfile system attribute specifies its path > to debug the configuration. Use < configuration status = "trace" > at the beginning of your configuration file > I suggest selecting one of the many sample configurations provided in the log4j2 manual, and then adding more ringtones and whistles bit by bit
If your problem is not one of the above, please display your configuration and provide more details about the problem you are experiencing (you are not sure what you expect from automatic configuration. If log4j2 cannot find the configuration file, this is a very basic function. You can log the error event to the console, which is rarely enough.)
To write to your home directory, you can use the system property lookup ${sys: propertyname} The following is an example configuration:
<Configuration status="trace"> <Properties> <Property name="logfile">${sys:user.home}/log${date:yyyyMMdd}.log</Property> </Properties> <Appenders> <Console name="STDOUT" target="SYstem_OUT"> <PatternLayout pattern="%m%n"/> </Console> <File name="FILE" fileName="${sys:logfile}"> <PatternLayout> <pattern>%d %p [%t] %c{1.} %m%n</pattern> </PatternLayout> </File> </Appenders> <Loggers> <Root level="trace"> <AppenderRef ref="STDOUT" level="ERROR" /> <AppenderRef ref="FILE" /> </Root> </Loggers> </Configuration>