https://github.com/qos-ch/logback-tyler
Logback XML configuration to Java translation project
https://github.com/qos-ch/logback-tyler
Last synced: about 2 months ago
JSON representation
Logback XML configuration to Java translation project
- Host: GitHub
- URL: https://github.com/qos-ch/logback-tyler
- Owner: qos-ch
- License: mit
- Created: 2024-02-12T09:38:33.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-30T21:39:21.000Z (5 months ago)
- Last Synced: 2025-03-31T06:51:11.698Z (3 months ago)
- Language: Java
- Size: 297 KB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
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.11.## 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
1.0.2
```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
package com.example;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.FileAppender;/**
*
*This file was generated by logback-tyler version 1.0.2
*
*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.
*
*/
public class TylerConfigurator extends TylerConfiguratorBase implements Configurator {
/**
* Appender variable referencing the appender named "TOTO".
*/
protected FileAppender appenderTOTO;/**
*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 loggerContext) {
setContext(loggerContext);
addOnConsoleStatusListener();
propertyModelHandlerHelper.handlePropertyModel(this, "APP_NAME", "myApp", "", "", "");
setContextName(subst("${APP_NAME}"));
this.appenderTOTO = setupAppenderTOTO();
Logger logger_ROOT = setupLogger("ROOT", "DEBUG", null);
logger_ROOT.addAppender(appenderTOTO);
return ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY;
}FileAppender setupAppenderTOTO() {
FileAppender appender = new FileAppender();
appender.setContext(context);
appender.setName("TOTO");
appender.setFile("toto.log");
appender.setAppend(true);
appender.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(appender);
patternLayoutEncoder.start();
// Inject component of type PatternLayoutEncoder into parent
appender.setEncoder(patternLayoutEncoder);appender.start();
return appender;
}
}
// 21:30:59,832 |-INFO in ch.qos.logback.tyler.base.handler.ImplicitModelHandler - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
// 21:30:59,833 |-INFO in ch.qos.logback.core.model.processor.DefaultProcessor@69998645 - End of configuration.
```