Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/qos-ch/logback-tyler

Logback XML configuration to Java translation project
https://github.com/qos-ch/logback-tyler

Last synced: 9 days ago
JSON representation

Logback XML configuration to Java translation project

Awesome Lists containing this project

README

        

# logback-tyler

Logback-tyler translates logback-classic XML configuration files into Java.

The resulting java class named `TylerConfigurator` implements the
[`Configurator`](https://logback.qos.ch/xref/ch/qos/logback/classic/spi/Configurator.html)
interface. It can thus be declared as a custom configuration provider
using Java's standard
[service-provider](https://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html)
meachanism. Custom configurators are searched by looking up a provider-configuration file
resource located under the
_META-INF/services/ch.qos.logback.classic.spi.Configurator_ file in
your project. This provider-configuration should contain a line with the fully
qualified class name of your desired configurator.

Running `TylerConfigurator` does not require XML parsers and usually
executes noticeably faster than `JoranConfigurator`, logback's XML
configurator. Moreover, `TylerConfigurator` does not use
reflection. In addition, given that it ships with your project's
binaries, it is harder to modify and offers an even smaller attack
surface.

At runtime, `TylerConfigurator` does not have any additional
dependencies other than logback-classic version 1.5.10.

## Online instance
A instance of logback-tyler translator is available [**online**](https://logback.qos.ch/translator/services/xml2Java.html).
Do not hesitate to experiment with it.

## Running the translator locally

Logback-tyler is located at the following Maven coordinates:
```xml
ch.qos.logback.tyler
tyler-base
0.9
```

Here is a sample program to translate a logback.xml as string into Java.

```java
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.util.StatusPrinter;

import java.io.IOException;

public class TylerExample {

String xmlInput =
"""





${APP_NAME}


toto.log
true
true

%-4relative [%thread] %-5level %logger{35} -%kvp- %msg%n








""";

public static void main(String[] args) throws JoranException, IOException {
ContextBase context = new ContextBase();
ModelToJava m2j = new ModelToJava(context);
Model model = m2j.extractModel(xmlInput);
String result = m2j.toJava(model);
System.out.println(result);
// print any error messages occuring during the translation
StatusPrinter.print(context);
}
}
```

running the above program will produce the following output

```java

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.Configurator;
import ch.qos.logback.classic.tyler.TylerConfiguratorBase;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.joran.spi.NoAutoStartUtil;
import ch.qos.logback.core.spi.ContextAware;
import ch.qos.logback.core.spi.LifeCycle;
import java.lang.Override;

/**
*
*

This file was generated by logback-tyler version 0.9.


*
*

Eventual errors and warnings are appended at the end.


*
*

You may experiment with logback.xml to Java translation, i.e.
* TylerConfigurator generation, at the following URL:


*
*

https://logback.qos.ch/translator/services/xml2Java.html


*
*

This generated TylerConfigurator class is intended to be copied and integrated
* into the user's project as a custom configurator. It will configure logback
* without XML. You are free to rename TylerConfigurator as you wish.


*
*

It requires logback-classic version 1.5.11 or later at runtime.


*
*

Custom configurators are looked up via Java's service-provide facility. If a
* custom provider is found, it takes precedence over logback's own configurators,
* e.g. DefaultJoranConfigurator.


*
*

To install your custom configurator to your project, add a
* provider-configuration file to the following path:


*
*
  META-INF/services/ch.qos.logback.classic.spi.Configurator

*
*

This provider-configuration file should contain a line with the fully
* qualified class name of your tyler configurator.


*
*

See also item 1 of 'Configuration at initialization' section at


*
*

https://logback.qos.ch/manual/configuration.html#auto_configuration


*
*

With recent versions of logback and logback-tyler you can still
* configure logger levels dynamically using properties files. Note that
* configuration files in properties format can be watched for
* changes. See the documentation on PropertiesConfigurator for more details.


*
*

https://logback.qos.ch/manual/configuration.html#propertiesConfigurator


*
*

Keep in mind that by integrating a .properties configuration file info
* your tyler configurator, you can still change logger levels dynamically, without
* redeploying your application.


*
*/
class TylerConfigurator extends TylerConfiguratorBase implements Configurator {
/**
*

This method performs configuration per {@link Configurator} interface.


*
*

If TylerConfigurator is installed as a configurator service, this
* method will be called by logback-classic during initialization.


*/
@Override
public Configurator.ExecutionStatus configure(LoggerContext loggerCoontext) {
setContext(loggerCoontext);
addOnConsoleStatusListener();
propertyModelHandlerHelper.handlePropertyModel(this, "APP_NAME", "myApp", "", "", "");
setContextName(subst("${APP_NAME}"));
Appender appenderRFILE = setupAppenderRFILE();
logger_ROOT.addAppender(appenderRFILE);
Logger logger_ROOT = setupLogger("ROOT", "DEBUG", null);
return ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY;
}

Appender setupAppenderRFILE() {
FileAppender appenderRFILE = new FileAppender();
appenderRFILE.setContext(context);
appenderRFILE.setName("RFILE");
appenderRFILE.setFile("toto.log");
appenderRFILE.setAppend(true);
appenderRFILE.setImmediateFlush(true);

// Configure component of type PatternLayoutEncoder
PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
patternLayoutEncoder.setContext(context);

patternLayoutEncoder.setPattern("%-4relative [%thread] %-5level %logger{35} -%kvp- %msg%n");
patternLayoutEncoder.setParent(appenderRFILE);
patternLayoutEncoder.start();

// Inject component of type PatternLayoutEncoder into parent
appenderRFILE.setEncoder(patternLayoutEncoder);

appenderRFILE.start();
return appenderRFILE;
}
}

----------------
15:44:51,087 |-INFO in ch.qos.logback.tyler.base.handler.ImplicitModelHandler - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
15:44:51,092 |-INFO in ch.qos.logback.core.model.processor.DefaultProcessor@1f760b47 - End of configuration.
```