Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!
like image 787
Pardha.Saradhi Avatar asked Oct 25 '25 04:10

Pardha.Saradhi


1 Answers

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.

like image 66
Monzurul Shimul Avatar answered Oct 26 '25 17:10

Monzurul Shimul