简体   繁体   中英

Encode pattern layout in apache log4j

Trying to use the following pattern layout to print encoded log messages, so that \\n will not be printed as a new line in the logs.

  <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern" value="%d{MM-dd-yyyy HH:mm:ss,SSS} %5p [%t] (%F:%L) - %enc{%m}%n"/>
  </layout>

But it literally prints %enc{ before every message and prints } at the end of the messages, instead of encoding them. \\n is also not being replaced.

Example: For this code logger.info("my \\n message") following is the output log

08-01-2018 01:26:37,642  INFO [localhost-startStop-1] (ClassName1.java:53) - %enc{my 
message}

instead it should be like,

08-01-2018 01:26:37,642  INFO [localhost-startStop-1] (ClassName1.java:53) - my message

Followed this document https://logging.apache.org/log4j/log4j-2.1/manual/layouts.html But I'm not sure what is wrong with the pattern.

You should determine the encoding format in curly brackets. For example %enc{%m}{HTML} encodes the message in HTML format. You could also use CRLF: enc{%m}{CRLF}

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