1

We use a JNDI-Property (set in Tomcat webserver) to determine the stage (DEV/TEST/QA/PRD) in order to configure some application-details.

Now we want to replace the homebrew-logging with an external tool and want to give tinylog a try. But we wonder if it is possible to read environment variables from JNDI context to configure tinylog settings?

The documentation says nothing about JNDI-lookups. Maybe the Java-based configuration might be solution. But what about the declarative textbased configuration?

Any advice appriciated! Thank you!

LWn
  • 13
  • 2
  • Which properties do depend on the stage? Do you have different properties file for each stage or do you configure tinylog via Java? Do you use tinylog 1.x or 2.x? – Martin May 17 '19 at 21:48
  • Hello Martin, thanks for your reply. Until now, we do not use tinylog at all, but we think it might be a valuable alternative to the other "big ones" like lockback/sl4j/etc. I think we would test tinylog 2. When will the first stable version 2.0 be released? We want to deploy all the properties within the WAR file and we used to call the server in order to detect the stage the application will be running for. We want to load a stage-dependent configuration which might be a different file for each stage or use switches within the configuration. Can you suggest a solution, a tinylog-way? – LWn May 20 '19 at 10:43
  • The first feature complete release candidate of tinylog 2.0 is already in progress. I expect to release the final version of tinylog 2.0 in a few months. – Martin May 20 '19 at 21:50

1 Answers1

0

tinylog is an universal logging library for all kind of Java applications. There is no native support for context lookups as it is a specific Java EE feature. However, you can load your custom tinylog configuration at startup via a ServletContextListener.

@WebListener
public class LoggingInitializer implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        try {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            String stage = (String) new InitialContext().lookup("java:comp/env/stage");
            String file = "tinylog_" + stage + ".properties";

            Properties configuration = new Properties();
            try (InputStream stream = classLoader.getResourceAsStream(file)) {
                configuration.load(stream);
            }

            Configuration.replace((Map) configuration);
        } catch (IOException | NamingException ex) {
            Logger.error(ex, "Failed to load tinylog configuration");
        }
    }

    public void contextDestroyed(ServletContextEvent event) { }

}

The stage can be set as environment variable in your context.xml:

<Context>
    <Environment name="stage" value="PROD" type="java.lang.String" />
    <!-- Other Settings -->
</Context>
Martin
  • 598
  • 1
  • 4
  • 27