简体   繁体   中英

Pattern for Slf4j logger

I am getting logs in the spring boots default pattern.

2017-02-10 15:39:01.111  INFO 24483 --- [ryBean_Worker-1] c.f.dashboard.services.SchedulerService  : Hello World!

I want to get the logs in this format

2017/02/10 11:24:37,771 [INFO] [http-nio-8080-exec-8] myMethod(myClass.java:38) - Hello World!

I have tried using this pattern

%sn %d{yyyy/MM/dd HH:mm:ss,SSS} %r [%-5p] [%t] %M(%F:%L) - %m%n

but is giving parse errors in the log lines.

%PARSER_ERROR[sn] 2017/02/10 09:41:25 12018 [INFO ] [schedulerFactoryBean_Worker-1] %PARSER_ERROR[M] - Hello World!

Add this in your application.xml

logging.pattern.console=%d{"yyyy/MM/dd HH:mm:ss,SSS"} [%p] [%t] %M\\(%F:%L\\) - %msg%n

Or in application.yml

logging:
  pattern:
    console: '%d{"yyyy/MM/dd HH:mm:ss,SSS"} [%p] [%t] %M\(%F:%L\) - %msg%n'

For date pattern, the comma ',' character is interpreted as the parameter separator, the pattern HH:mm:ss,SSS will be interpreted as the pattern HM:mm:ss and the timezone SSS. If you wish to include a comma in your date pattern, then simply enclose the pattern between quotes. For example, %date{"HH:mm:ss,SSS"}

For method pattern, If you need to treat the parenthesis character as a literal, it needs to be escaped by preceding each parenthesis with backslash. Otherwise you will get parser error.

Rests are self explanatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM