Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8245291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:07:56+00:00 2026-06-07T22:07:56+00:00

I am writing a simple Groovy application that needs to do some logging. The

  • 0

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 instance, while developing I just want to log to the console, in test and production I may want to write to a file and in production I may want to send email for the most severe events.

Right now what I am doing looks like this:

import org.apache.log4j.Logger
import org.apache.log4j.PropertyConfigurator

class BaseClass {
    protected config

    static Logger logger = Logger.getLogger(BaseClient.class)

    def BaseClass(env) {
        def configFilePath = // whatever

        config = new JsonSlurper().parseText(configFile.text)[options.env]

        def logConfigFilePath = ['somelogdir', config.log_file].join(File.separator)
        PropertyConfigurator.configure(logConfigFilePath)
    }
}

and then all my classes that need to do logging inherit from BaseClass.

In this way, I can specify a different filename for each environment, and I get to read the logging configuratio from there. But it seems a lot of boilerplate, and forces me to use a hierarchy that may not be ideal.

On the other hand, here I see that I can obtain a logger with a simple annotation.

Is there a way to get different loggers depending on the environment – which may be set at runtime?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:07:59+00:00Added an answer on June 7, 2026 at 10:07 pm

    Building on tim_yates’s answer, use a groovy file to configure log4j. For example:

    // log4j.groovy
    log4j {
        rootLogger="DEBUG, A1"
        appender.A1 = "org.apache.log4j.ConsoleAppender"
        appender.'A1.layout' = "org.apache.log4j.PatternLayout"
    
        if (System.properties['env'] == 'prod') {
            appender.'A1.layout.ConversionPattern'="prod %-4r [%t] %-5p %c %x - %m%n"
       } else {
            appender.'A1.layout.ConversionPattern'="dev %-4r [%t] %-5p %c %x - %m%n"
       }
    }
    

    Then make your script use it:

    @GrabConfig(systemClassLoader=true)
    @Grab(group='log4j', module='log4j', version='1.2.17')
    
    import groovy.util.logging.Log4j
    import org.apache.log4j.PropertyConfigurator
    
    @Log4j
    class Test {
        def dosome() {
            log.info('Logging!')
        }
    
        static main( args ) {
            def config = new ConfigSlurper().parse(new File('log4j.groovy').toURL())
            PropertyConfigurator.configure(config.toProperties())
    
            new Test().dosome()
        }
    }
    

    And finally, launch your program with the environment in a system property:

    groovy -Denv=prod Test.groovy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I writing some simple application that will look for some message ( sms message
I writing simple application that need to make some record of the incoming sound.
I'm writing simple GUI using wxPyhon and faced some problems. My application does simple
Writing a simple program that will find exact duplicate files on my computer, but
I am writing a Groovy script that needs to POST JSON to a URL.
Here's the situation: I'm writing a simple AJAX application that performs CRUD functions. When
I'm writing a simple OpenGL application that uses GLUT . I don't want to
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I am writing simple site that requires users and profiles to be handled. The
While writing a simple server-client application, this question came in my mind. When someone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.