log4j是否支持JSON格式?

时间:2015-04-01 09:17:34

标签: java json log4j

是否可以通过仅更改log4j.properties.xml配置文件让log4j以JSON格式输出日志记录?
我使用了一个使用log4j 1.2的旧应用程序。我只看到XML布局但没有JSON布局。

4 个答案:

答案 0 :(得分:29)

这是官方的JSON布局

https://github.com/logstash/log4j-jsonevent-layout

1)添加maven依赖https://mvnrepository.com/artifact/net.logstash.log4j/jsonevent-layout

<dependency>
    <groupId>net.logstash.log4j</groupId>
    <artifactId>jsonevent-layout</artifactId>
    <version>1.7</version>  
</dependency>

2)将配置添加到log4j.properties文件

 log4j.rootCategory=WARN, RollingLog
 log4j.appender.RollingLog=org.apache.log4j.DailyRollingFileAppender
 log4j.appender.RollingLog.Threshold=TRACE
 log4j.appender.RollingLog.File=api.log
 log4j.appender.RollingLog.DatePattern=.yyyy-MM-dd
 log4j.appender.RollingLog.layout=net.logstash.log4j.JSONEventLayoutV1

答案 1 :(得分:21)

只需使用buildin PatternLayout即可:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.encoding=UTF-8
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern={"debug_level":"%p","debug_timestamp":"%d{ISO8601}","debug_thread":"%t","debug_file":"%F", "debug_line":"%L","debug_message":"%m"}%n

会像:

{
    "debug_level" : "INFO",
    "debug_timestamp" : "2016-05-26 16:37:08,938",
    "debug_thread" : "main",
    "debug_file" : "TestLogOutPut.java",
    "debug_line" : "316",
    "debug_message" : "hello i am a log message"
}

答案 2 :(得分:2)

是的,这是可能的。 Take a look at this link 它可以生成

{
   "timestamp":1352412458890,
   "date":"Nov 8, 2012 10:07:38 PM",
   "hostname":"michael1",
   "username":"michael",
   "level":"INFO",
   "thread":"main",
   "classname":"uk.me.mjt.log4jjson.SimpleJsonLayoutTest",
   "filename":"SimpleJsonLayoutTest.java",
   "linenumber":25,
   "methodname":"testDemonstration",
   "message":"Example of some logging"
 }

答案 3 :(得分:0)

我使用以下模式的logback.xml

import UIKit
import CountdownLabel

class ViewController: UIViewController { 

    @IBOutlet weak var moreBtn: UIButton!
    @IBOutlet weak var lessBtn: UIButton!
    @IBOutlet weak var gifview: UIImageView!
    @IBOutlet weak var countdownLabel: CountdownLabel!
    var mins = 25
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCountdown()
        updateTimer(min: mins)
    }
    
    // MARK: Buttons
    
    @IBAction func startPressed(_ sender: Any) {
        countdownLabel.start()
    }
    @IBAction func lessPressed(_ sender: Any) {
        if(mins > 0){
        mins = mins - 5
        updateTimer(min: mins)
        }
    }
    @IBAction func morePressed(_ sender: Any) {
        mins = mins + 5
        updateTimer(min: mins)
    }
    
    
    
    //MARK: Helper Func
    
    func updateTimer(min: Int){
        countdownLabel.setCountDownTime(minutes: 60*Double(min))
    }
    
    func setupCountdown(){
        countdownLabel.textColor = .black
        countdownLabel.font = UIFont(name:"Courier", size:UIFont.labelFontSize)
        countdownLabel.animationType = .Evaporate
        countdownLabel.countdownDelegate = self  //>>>>>> added this line to your code
    }
}

//>>>>>>added this function to your code
extension ViewController: CountdownLabelDelegate {
    func countdownFinished() {
        debugPrint("countdownFinished at delegate.")
    }
}

并以json格式生成日志。我正在使用带有springboot依赖项的log4j。

<appender name="Console"
          class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            {"level":"%p","timestamp":"%d{ISO8601}","thread":"%t","file":"%F", "line":"%L","message":"%m"}%n
        </Pattern>
    </layout>
</appender>