问题描述
IM创建一个日志文件系统.日志文件将以JSON格式进行,因此服务器可以以后阅读,但我认为这太重要了.我需要知道的是,可以将log4j配置为将其写入文件,但没有任何标签,例如文件中的信息,调试,时间戳等.我在这里看
但是,这与其他内容一起对文件进行了努力.我只需要写的数据以显示在文件中.如果达到最大大小后,我还想在文件上设置某种文件旋转.
推荐答案
使用log4j.properties配置文件(将其放在classPath的顶部,log4j将"只是找到它"):
# This is the default logger, simply logs to console log4j.logger.com.foo.bar=DEBUG,A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout # Note the Pattern here, emits a lot of stuff - btw, don't use this in production # %C is expensive - see the Javadoc for ConversionPattern for the meaning of all # the % modifiers: log4j.appender.A1.layout.ConversionPattern=%d{MMM dd, HH:mm:ss} [%C{2}] %-5p - %m%n # Logging to file can be enabled by using this one log4j.logger.com.example=DEBUG, R log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=/var/log/generic.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=1 # This is the most minimalist layout you can have: just the 'm'essage is emitted # (and a \n newline): log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%m%n
com.foo.bar软件包中的所有类(和子包)都将记录到控制台,com.example(及以下)中的所有类将记录到/var/log/generic.log.
.要发射JSON,只需使用Jackson(com.fasterxml)将数据转换为JSON对象并将其写入字符串.
其他推荐答案
您想要的是 tatter 仅使用%m%n,并与先前提出的问题的答案相结合在这里
其他推荐答案
您应该能够编写自定义日志附录器,我对MongoDB有一个模糊的回忆. MongoDD将其数据存储为JSON,因此它已将日志转换为JSON格式.
我将首先搜索Mondgodb Appender,并查看已发货的文件Appender,如果一个人尚不存在,则应该为您提供一个appender的起点.
问题描述
Im creating a log file system. the log file will be in json format so a server can read it after but i dont think thats too important. What i need to know is can log4j be configured to write into to a file but without any tags like info,debug, timestamp etc in the file. I have looked here
but this polutes the file with with other things. I want ONLY the data i write to show up in the file. I'd also like to set some kind of file rotation on the file if it gets too big after a max size is reached.
推荐答案
This is relatively easy, using a log4j.properties configuration file (place it at the top of your classpath, and Log4j will 'just find it'):
# This is the default logger, simply logs to console log4j.logger.com.foo.bar=DEBUG,A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout # Note the Pattern here, emits a lot of stuff - btw, don't use this in production # %C is expensive - see the Javadoc for ConversionPattern for the meaning of all # the % modifiers: log4j.appender.A1.layout.ConversionPattern=%d{MMM dd, HH:mm:ss} [%C{2}] %-5p - %m%n # Logging to file can be enabled by using this one log4j.logger.com.example=DEBUG, R log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=/var/log/generic.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=1 # This is the most minimalist layout you can have: just the 'm'essage is emitted # (and a \n newline): log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%m%n
All the classes in the com.foo.bar package (and subpackages) will log to console, those in com.example (and below) will log to /var/log/generic.log.
To emit JSON, just use Jackson (com.fasterxml) convert your data to a JSON object and write it out as a string.
其他推荐答案
What you want is the PatternLayout with %m%n only, and combine with the answer to previously asked question here
其他推荐答案
You should be able to write a custom log appender, I have a vague recollection of there being an example to do this with MongoDB. MongoDD stores its data as JSON, so it have converted the log to json format before inserting it.
I would start by searching for a MondgoDB appender, and looking at the file appender that is shipped, that should give you a starting point for an appender if one doesnt already exist.