如何仅从 main 方法写入日志

How to write logs only from main method

我已经将 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

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

只需稍微修改 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

...