Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
[Requirement] Print logs to the console
The demand my girlfriend received today was to test the execution time of a method, and then output the current long value in the first and last lines of this method, so as to determine the execution time of the method.
However, there is still a problem, the log printed on the test is fine, as soon as the result of the production environment, it is dead, there is no relevant log file in the main log file, sad.
[Problem] The log component is printed differently in the test environment and production environment
In this case, the log printing inconsistency between the production environment and the test environment can be easily located. You only need to check which component logs are used, determine the configuration file of the log component, and check whether the printing rules are the same in the production environment and the test environment.
This may not be the case either, but it happens to be the solution to her current problem, albeit a bit mindless.
【 results 】
The use of the logback component depends on the configuration rules in Logger. XML, which generally assign different printing rules to different environments.
Let me outline the configuration of logback, which is a common rule in my project.
<? The XML version = "1.0" encoding = "utf-8"? > <configuration> <contextName>logback</contextName> <springProperty scope="context" name="logPath" source="logging.path"/> <property name="log.path" value="${logPath}" /> <! - the output to the CONSOLE - > < appender name = "CONSOLE" class = "ch. Qos. Logback. Core. ConsoleAppender" > < filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>info</level> </filter> <encoder> <Pattern>${CONSOLE_LOG_PATTERN}</Pattern> <charset>UTF-8</charset> </encoder> </appender> <! - time rolling output level for the INFO log - > < appender name = "INFO_FILE" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > <file>${log.path}/log_info.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> <charset>UTF-8</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>30</maxHistory> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>info</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <springProfile name="test"> <logger name="com.wingtra.ymt" level="debug"/> <root level="debug"> <appender-ref ref="CONSOLE"/> </root> </springProfile> <springProfile name="dev"> <logger name="com.wingtra.ymt" level="debug"/> <root level="debug"> <appender-ref ref="CONSOLE"/> <appender-ref ref="INFO_FILE"/> </root> </springProfile> <springProfile name="pro"> <root level="info"> <appender-ref ref="INFO_FILE"/> </root> </springProfile> </configuration>Copy the code
This is the logback.xml configuration, where springProfile is used to specify the environment parameters.