Solution for mybatis plus unable to output through logback spring
- 2021-12-11 17:59:09
- OfStack
Problem description
Through official website configuration, mybatis-plus is introduced into spring boot project, but the log can only be output in the console, but not in the log file of logback
// Specific reference website
// https://mp.baomidou.com/guide/faq.html#%E5%90%AF%E5%8A%A8-mybatis-%E6%9C%AC%E8%BA%AB%E7%9A%84-log-%E6%97%A5%E5%BF%97
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Solving process
1. Click to enter the StdOutImpl method to view. This method is printed through System. out. println, and logback cannot output this content without configuration.
2. System. out. println is output in logback printed file, and I found three methods
Method 1:
System.setOut(new PrintStream(new File(" Log path ")));
System.out.println();
Method 2: Use the jar package
<dependency>
<groupId>uk.org.lidalia</groupId>
<artifactId>sysout-over-slf4j</artifactId>
<version>1.0.2</version>
</dependency>
web. xml file
<listener>
<listener-class>uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4JServletContextListener</listener-class>
</listener>
Method 3: The spring-boot project uses the nohup command to start the unspecified log file output, and all logs are output to nohup. out. You can write an shell script regularly, cut the contents of nohup. out according to the date, and discard the log file output by logback
this_path=$(cd `dirname $0`;pwd)
cd $this_path
echo $this_path
current_date=`date -d "-1 day" "+%Y%m%d"`
echo $current_date
// Split the specified character size into a new file
split -b +100m -d -a 4 /home/.../nohup.out /home/.../log/log_${current_date}_
// Empty nohup.out Wait a minute 1 Sub-partition
cat /dev/null > nohup.out
None of the above three methods are suitable for my current needs, so I need to solve this problem from a different angle, for example, giving up using StdOutImpl to output logs
Solutions
Discard the StdOutImpl output log and remove the log-impl: org. apache. ibatis. logging. stdout. StdOutImpl configuration. Use the regular logback-spring configuration in application. yml.
logging:
level:
com.XXX.mapper: debug
You can output the sql statement normally.