Logback log file multi-environment configuration path
When encountering problems in the project, after the local jar package of the springboot project is deployed to the site, it is often encountered that the path of log storage on site will be changed. After reviewing, there are two ways. Let me briefly explain it below.
1. The first type: Add parameters when starting the jar package
--=F:\hgtest\config\, adopts the specified log configuration file.
start javaw -jar F:\hgtest\ --=F:\hgtest\config\ --=F:\hgtest\config\
2. Multi-environment configuration log storage path
Starting the jar will determine which log path to save the log based on the value.
1. Add dependencies to facilitate the use of <springProperty> tags
<!-- Logback Classic Module --> <dependency> <groupId></groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <!-- Use the appropriate version number --> </dependency>
2. Modify file configuration
Remark:
- The difference between
- Both logback and logback can be used to configure logback, but the loading order of the two is different.
- —>—>.
- Therefore, if you have variables configured, the value will not be obtained, so it is recommended to use it as much as possible in Springboot.
3. Modify Add path variable reading
<!-- Log storage path --> <springProperty scope="context" name="" source="" defaultValue="./logs" /> <!-- System log output --> <appender name="file_info" class=""> <file>${}/</file> <!-- Circular Policy:Create log files based on time --> <rollingPolicy class=""> <!-- Log file name format --> <fileNamePattern>${}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- The biggest history of logs 15sky --> <maxHistory>15</maxHistory> </rollingPolicy> <encoder> <pattern>${}</pattern> </encoder> <filter class=""> <!-- Filtering level --> <level>INFO</level> <!-- Operations during matching:take over(Record) --> <onMatch>ACCEPT</onMatch> <!-- 不Operations during matching:reject(不Record) --> <onMismatch>DENY</onMismatch> </filter> </appender>
Here is a springProperty configured to obtain file addresses from and a default address is configured.
-
name
: The variables that can be used in : correspond to the variable values quoted below. -
source
: Variables configured in -
defaultValue
: The default value when the variables configured in cannot be obtained (it is recommended to directly configure the location of the production environment)
After this configuration, the log log can be saved to the specified directory. Personal test available.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.