【问题标题】:How to write logs only from main method如何仅从主要方法写入日志
【发布时间】:2020-01-20 09:40:51
【问题描述】:

我已将 log4j 添加到我的 groovy 脚本中,在其中我使用 httpBuilder 发出一些请求并尝试记录必要的信息,但是当日志级别为 DEBUG 时,我会看到来自我不需要的 HttpBuilder 方法的日志。我怎样才能只写我想要的日志?

我的意思是我

main()
def main() {
    Log4JLogger logger = new Log4JLogger('LOGGERRR')
    logger.info("!!!!!!!!!!!!!!!!!!")
    def httpReq = new HTTPBuilder("${url}")
    httpReq.request(Method.GET, ContentType.JSON) {
    headers.'Authorization' = "Basic ${authString}"         
           response.success = { resp ->
               println("Ok")
           }
           response.failure = { resp ->
               println("Not ok")
           }   
}

这里是 log4j.properties

log4j.rootLogger=DEBUG, file

# Appender 
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
# Path
log4j.appender.file.File=./log_file.log

log4j.appender.file.Append=false
log4j.appender.file.DatePattern='.'yyyy-MM-dd-a

# configuring pattern
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t]:%L - %m%n

这是日志文件的一部分

2020-01-20 17:25:57 INFO  [main]:? - !!!!!!!!!!!!!!!!!!
2020-01-20 17:26:28 DEBUG [main]:449 - GET https://some url
2020-01-20 17:26:30 DEBUG [main]:161 - Get connection for route {s}->url:port
2020-01-20 17:26:30 DEBUG [main]:177 - Connecting to url:port
2020-01-20 17:26:32 DEBUG [main]:123 - CookieSpec selected: default
2020-01-20 17:26:32 DEBUG [main]:77 - Auth cache not set in the context
2020-01-20 17:26:32 DEBUG [main]:89 - Proxy auth state: UNCHALLENGED
2020-01-20 17:26:32 DEBUG [main]:682 - Attempt 1 to execute request
2020-01-20 17:26:32 DEBUG [main]:274 - Sending request: GET url HTTP/1.1
2020-01-20 17:26:32 DEBUG [main]:73 -  >> "GET url/1.1[\r][\n]"
2020-01-20 17:26:32 DEBUG [main]:73 -  >> "Accept: application/json, application/javascript, text/javascript[\r][\n]"
2020-01-20 17:26:32 DEBUG [main]:73 -  >> "Authorization: Basic authstring[\r][\n]"
2020-01-20 17:26:32 DEBUG [main]:73 -  >> "Host: jurl[\r][\n]"
2020-01-20 17:26:32 DEBUG [main]:73 -  >> "Connection: Keep-Alive[\r][\n]"
2020-01-20 17:26:32 DEBUG [main]:73 -  >> "[\r][\n]"
2020-01-20 17:26:32 DEBUG [main]:278 - >> GET url HTTP/1.1
2020-01-20 17:26:32 DEBUG [main]:281 - >> Accept: application/json, application/javascript, text/javascript
2020-01-20 17:26:32 DEBUG [main]:281 - >> Authorization: Basic authstring
2020-01-20 17:26:32 DEBUG [main]:281 - >> Host: 
2020-01-20 17:26:32 DEBUG [main]:281 - >> Connection: Keep-Alive
2020-01-20 17:26:32 DEBUG [main]:73 -  << "HTTP/1.1 200 200[\r][\n]"
.... and so on

但我只需要将第一个字符串写入日志文件。

【问题讨论】:

    标签: groovy log4j


    【解决方案1】:

    只需稍微修改log4j.properties 并仅为您命名为LOGGERRR 的记录器指定DEBUG 级别:

    # set default logger to ERROR level
    log4j.rootLogger=ERROR, file
    # set your custom logger to DEBUG level
    log4j.logger.LOGGERRR=DEBUG, file
    
    ...
    

    【讨论】:

    • 非常感谢。我还添加了log4j.additivity.LOGGERRR=false,因为我将所有信息记录了两次。为什么没有字符串数量,而是有“?”,我添加了 %L log4j.appender.file.layout.ConversionPattern
    • 我在 Intellij 的运行/调试配置中选择了“启用调试 stck 跟踪”,但它仍然不起作用
    • 有一个类似的问题(关于未知的行号)但关于 logback 记录器:stackoverflow.com/questions/59491564/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2010-09-15
    相关资源
    最近更新 更多