java - Log4J - JsonLayout and RollingFileAppender generating invalid JSON -
i'm trying store logs in json files using log4j (2.6.2). i'm using jsonlayout
in rollingfileappender
, , it's working long i'm not trying append file has been written previously.
here's code setting layout , appender:
layout<?> layout = jsonlayout.createlayout(config, false, false, false, true, false, true, "[", "]", charset.defaultcharset()); string appenderfilename = "mylogfile-latest.log.json"; string appenderfilepattern = "mylogfile-%i.log.json"; string appendername = "myappender"; appender appender = rollingfileappender.createappender(appenderfilename, appenderfilepattern, "true", appendername, "true", "256", "true", sizebasedtriggeringpolicy.createpolicy(this.configuration.getlogmaxsize().tostring()), null, layout, null, "false", "false", null, config);
as said, it's working fine first time i'm writing in log file:
[ { "timemillis" : 1469620840442, "thread" : "simpleasynctaskexecutor-43", "level" : "error", "loggername" : "mylogger", "message" : "my log message", "endofbatch" : false, "loggerfqcn" : "org.apache.logging.log4j.spi.abstractlogger", "threadid" : 243, "threadpriority" : 5 } , { "timemillis" : 1469620840442, "thread" : "simpleasynctaskexecutor-43", "level" : "debug", "loggername" : "mylogger", "message" : "my log message", "endofbatch" : false, "loggerfqcn" : "org.apache.logging.log4j.spi.abstractlogger", "threadid" : 243, "threadpriority" : 5 } ]
i close appender, etc... , relaunch application, , when i'm writing new logs existing file, here's i'm getting:
[ { "timemillis" : 1469620840442, "thread" : "simpleasynctaskexecutor-43", "level" : "error", "loggername" : "mylogger", "message" : "my log message", "endofbatch" : false, "loggerfqcn" : "org.apache.logging.log4j.spi.abstractlogger", "threadid" : 243, "threadpriority" : 5 } , { "timemillis" : 1469620840442, "thread" : "simpleasynctaskexecutor-43", "level" : "debug", "loggername" : "mylogger", "message" : "my log message", "endofbatch" : false, "loggerfqcn" : "org.apache.logging.log4j.spi.abstractlogger", "threadid" : 243, "threadpriority" : 5 } ] { "timemillis" : 1469620840490, "thread" : "simpleasynctaskexecutor-43", "level" : "error", "loggername" : "mylogger", "message" : "my log message", "endofbatch" : false, "loggerfqcn" : "org.apache.logging.log4j.spi.abstractlogger", "threadid" : 245, "threadpriority" : 5 } , { "timemillis" : 1469620840492, "thread" : "simpleasynctaskexecutor-43", "level" : "debug", "loggername" : "mylogger", "message" : "my log message", "endofbatch" : false, "loggerfqcn" : "org.apache.logging.log4j.spi.abstractlogger", "threadid" : 245, "threadpriority" : 5 } ]
this issue makes impossible parse logs gson fail create list
file.
do have idea on how fix issue without doing crappy hack removing ]
, replacing comma?
thanks!
the reason why jsonlayout create "well-formed" json document. need put each document in own file (with start , end brackets) true, isn't case here.
you have 2 options:
- for jsonlayout, set complete=false (you have true in sample) print out without '[' , ']'. can add these chars before calling gson (as file not have array).
- create own jsonlayout (you need extend abstractstringlayout) using simple org.json:json or gson string file.
Comments
Post a Comment