0

I've added log4j into my groovy script where I use httpBuilder to make some requests and try to log necessary information but when log level is DEBUG I see logs from, I suppose, HttpBuilder methods which I don't need. How can I write log only which I want?

I mean that I

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")
           }   
}

Here is 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

Here is a part of log file

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

But I need to write only first string in to log file.

Duxa
  • 55
  • 1
  • 7

1 Answers1

1

just modify a bit the log4j.properties and specify DEBUG level only for your logger that you named LOGGERRR:

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

...
daggett
  • 26,404
  • 3
  • 40
  • 56
  • Thanks a lot. I've also added ```log4j.additivity.LOGGERRR=false``` because I had all information logged twice. And why there no number of string and instead it there is "?", I've added %L ```log4j.appender.file.layout.ConversionPattern``` – Duxa Jan 21 '20 at 02:46
  • I've choosen "Enable debug stck trace" in Run/Debug configuration in Intellij but it still doesn't work – Duxa Jan 21 '20 at 05:14
  • there was a similar question (about unknown line numbers) but about logback logger: https://stackoverflow.com/questions/59491564/logback-doesnt-print-method-or-line-number/59504563#59504563 – daggett Jan 21 '20 at 09:18