Groovy - configuring logging properties depending on environment

后端 未结 2 1931
猫巷女王i
猫巷女王i 2021-01-15 18:59

I am writing a simple Groovy application that needs to do some logging. The actual properties of how things are logged will depend on the particular environment. For instanc

2条回答
  •  花落未央
    2021-01-15 19:59

    You should be able to simply put a log4j.properties file on your classpath.

    Given this test script:

    @groovy.util.logging.Log4j
    class Test {
      def dosome() {
        log.info( 'Logging!' )
      }
    
      static main( args ) {
        new Test().dosome()
      }
    }
    

    If I run it from the command line:

    groovy -cp log4j-1.2.17.jar Test.groovy
    

    it doesn't print anything as it isn't set up to log INFO messages by default.

    However, if I write a log4j.properties file (taken from the docs for log4j and altered to log ):

    log4j.rootLogger=DEBUG, A1
    
    log4j.appender.A1=org.apache.log4j.ConsoleAppender
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    
    # Print the date in ISO 8601 format
    log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
    

    Then run it again with this properties file in the classpath:

    groovy -cp log4j.properties:log4j-1.2.17.jar Test.groovy 
    

    We get:

    2012-07-16 16:08:47,524 [main] INFO  Test - Logging!
    

提交回复
热议问题