Log4j将json数组写入磁盘 [英] Log4j to write json array to disk

查看:86
本文介绍了Log4j将json数组写入磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个日志文件系统.日志文件将为json格式,因此服务器可以在以后读取它,但我认为这不太重要.我需要知道的是,可以将log4j配置为写入文件,但在文件中没有任何标签,例如info,debug,timestamp等.我看过这里

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.

推荐答案

使用log4j.properties配置文件(将其放在类路径的顶部,Log4j会找到")相对简单: /p>

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

com.foo.bar包(和子包)中的所有类都将记录到控制台,com.example(及以下)中的类将记录到/var/log/generic.log.

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.

要发出JSON,只需使用Jackson(com.fasterxml)将数据转换为JSON对象并以字符串形式写出即可.

To emit JSON, just use Jackson (com.fasterxml) convert your data to a JSON object and write it out as a string.

这篇关于Log4j将json数组写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆