Setting method of general storage location for log4j log files in Linux / MAC / windows


The configuration of category in log4j 1 / log4j 2 and the output location of log

1、 Scenarios and requirements

Suppose I have three independent projects (temporarily associated with maven, of course, Maven is not used). One is the infrastructure providing public services, one is the persistence providing storage, and the other is the searcher providing search, in which all classes providing basic services, such as dateutils, are provided, Httputils and other tool classes are located in the directory com.chuanliu.platform . activity, and all classes that provide storage services are located in com.chuanliu.platform . activity.persist Directory, all classes providing search services are located in the com.chuanliu.platform . activity.solr Next.

Now the requirement of the project is to have a log file called full.log , you need to record all the logs, no matter under infrastructure, persist or server. In addition, you need a log file called solr.log To record all the logs in the search directory. Of course, the logs in the searcher directory should be placed in the searcer.log In the middle.

In addition, we should also support setting different log output locations according to different deployment environments, because windows and Linux have different directory structures.

2、 No matter log4j1 or log4j2, the following configuration can be used:

log4j.rootLogger = info
log4j.debug = false = info, full, stdout
#The child logger appender will not inherit the parent logger's appender, or the logs will be output twice in the file = false = info, solr = false

###### activity full log: contains all of the log ####
log4j.appender.full = org.apache.log4j.DailyRollingFileAppender
log4j.appender.full.File = ${LogFile.Location}/full.log
log4j.appender.full.DatePattern = '.'yyyy-MM-dd
log4j.appender.full.layout = org.apache.log4j.PatternLayout
log4j.appender.full.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
log4j.appender.full.ImmediateFlush = true
log4j.appender.full.append = true

######activity full log ####

######activity solr log : only contain the log in searcher####
log4j.appender.solr = org.apache.log4j.DailyRollingFileAppender
log4j.appender.solr.File = ${LogFile.Location}/solr.log
log4j.appender.solr.DatePattern = '.'yyyy-MM-dd
log4j.appender.solr.layout = org.apache.log4j.PatternLayout
log4j.appender.solr.layout.conversionPattern = %d [%t] %-5p %c{1} - %m%n
log4j.appender.solr.ImmediateFlush = true
log4j.appender.solr.append = true
######activity solr log ####

# Console output...
log4j.appender.stdout.layout.ConversionPattern=[%d][%p]%t: %m%n

# log sql for mybatis 
log4j.logger.mybatis =TRACE


In this way, after testing, you will find that all the logs are entered full.log In, but only com.chuanliu.platform . activity.solr The log output in the solr.log .

In addition, for the output location of the log${ LogFile.Location }, which can be customized in the configuration files of different environments LogFile.Location For example, in the In, definition LogFile.Location=C ://logs

The In, definition LogFile.Location=/usr/local/var/logs In the production environment, the same value is defined as: LogFile.Location=/usr/local/var/logs In this way, different log files will be generated under different paths in different environments.

In fact, after testing, there is no such trouble. We can uniformly configure the log output of windows and Linux to the same place, that is, we can uniformly set the output to / usr / local / var / logs. This setting is understandable in Linux, but it may not be easy to understand in Windows. In windows, / usr / local / var / log has two meanings:

1、If you are implementing the methods of these log output classes in eclipse, you will create the directory / usr / local / var / log on the disk where the workspace is located, such as disk e, and output the log full.log and solr.log .

2、If you are using the output of execution log in tomcat, you can create the directory / usr / local / var / log on the disk where Tomcat is located, such as disk D, and output it full.log and solr.log , Even if you start Tomcat in eclipse, the wtpapps directory of Tomcat is in the directory of eclipse, such as disk e, but the Tomcat you really use is in disk D. then you can also output the above log file in / usr / local / var / logs.

The above log4j log file Linux / MAC / windows general storage location setting method is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developer more.