Lib-Logger
is a library for easy
logging with the Apache Log4j 2 in a
Java(FX) & Maven desktop application.
Image: UML Lib-Logger
Hint
TheUML
diagram is created with theOnline Modeling Platform
GenMyModel.
In this example I want to show you how to configure a Java or JavaFX project
for the usage from the library Lib-Logger
.
- First move the file
log4j2-template.xml
from the GitHub project into the default packagesrc/main/resources
in your project. - Then rename the file to 'log4j2.xml'.
- Open the file and tweak it for your necessities. Plz see for more informations:
- If your project is hosted on GitHub or another development platform then don't
forgot to add the folder
/log/
to your.gitignore
file.
In the first example the developer can see how to configure a project for logging
purpose. With this requirements its now possible to log normal
messages like:
Example how to log a debug
message
public static final void loadResourcesInCache() {
LoggerFacade.getDefault().debug(TemplateLoader.class, "Load resources in cache"); // NOI18N
...
}
// which will print in the console and in the configured `xy.log` file:
2017-05-27 08:56:53,757 DEBUG Load resources in cache [TemplateLoader]
Example how to log a info
message
@Override
public void initialize(URL location, ResourceBundle resources) {
LoggerFacade.getDefault().info(this.getClass(), "Initialize ApplicationPresenter"); // NOI18N
...
}
// which will print in the console and in the configured `xy.log` file:
2017-05-27 08:56:55,073 INFO Initialize ApplicationPresenter [ApplicationPresenter]
Example how to log a error
message
private static String getResource(String name) {
String loadedResource = null;
try {
final URL url = new URL(name);
loadedResource = getResource(url.openStream());
} catch(IOException ex){
LoggerFacade.getDefault().error(ProjectCollector.class, "Error read resources: " + name, ex); // NOI18N
}
return loadedResource;
}
This example will show you how to log a starting message with following statements:
public class StartApplication extends Application implements IApplicationConfiguration {
@Override
public void init() throws Exception {
PropertiesFacade.getDefault().register(KEY__APPLICATION__RESOURCE_BUNDLE);
PropertiesFacade.getDefault().register(ITemplateConfiguration.KEY__TEMPLATE__RESOURCE_BUNDLE);
final char borderSign = this.getProperty(KEY__APPLICATION__BORDER_SIGN).charAt(0);
final String message = this.getProperty(KEY__APPLICATION__MESSAGE_START);
final String title = this.getProperty(KEY__APPLICATION__TITLE) + this.getProperty(KEY__APPLICATION__VERSION);
LoggerFacade.getDefault().message(borderSign, 80, String.format(message, title));
...
}
...
}
// which will print in the console and in the configured `xy.log` file:
2017-05-27 08:56:53,688 DEBUG Load properties: /com/github/naoghuman/demo/template/application/application.properties [LibProperties]
2017-05-27 08:56:53,705 DEBUG Load properties: /com/github/naoghuman/demo/template/template.properties [LibProperties]
2017-05-27 08:56:53,706 INFO
################################################################################
Start Demo-Template v0.1.0-SNAPSHOT.
################################################################################ [Logger]
where the constants
defined in
public interface IApplicationConfiguration {
public static final String KEY__APPLICATION__BORDER_SIGN = "application.border.sign"; // NOI18N
public static final String KEY__APPLICATION__MESSAGE_START = "application.message.start"; // NOI18N
public static final String KEY__APPLICATION__RESOURCE_BUNDLE = "/com/github/naoghuman/demo/template/application/application.properties"; // NOI18N
public static final String KEY__APPLICATION__TITLE = "application.title"; // NOI18N
public static final String KEY__APPLICATION__VERSION = "application.version"; // NOI18N
...
}
and the key-value pairs in application.properties
:
# Application
application.border.sign=#
application.message.start=Start %s.
application.message.stop=Stop %s.
# This values will be replaced during startup from the application.
# The format from the title in the application will be: ${pom.name} v${pom.version}
# Be aware from the empty sign between the two parameters.
application.build.datetime=${timestamp}
application.title=${pom.name}
application.version=v${pom.version}
The JavaDoc from the library Lib-Logger
can be explored here: JavaDoc Lib-Logger
Current version
is 0.7.0
. Main points in this release are:
- Prepare the library for
Java-11
! - Connect the GitHub project to Travis CI.
- Move the
JavaDoc
to the GitHub folderdocs/apidocs
.
Maven coordinates
In context from a Maven project you can use following maven coordinates:
<dependencies>
<dependency>
<groupId>com.github.naoghuman</groupId>
<artifactId>lib-logger</artifactId>
<version>0.7.0</version>
</dependency>
</dependencies>
Download manuell
- Release v0.7.0 (05.02.2019 / MM.dd.yyyy)
An overview about all existings releases can be found here:
- On your system you need JRE 8 or JDK 8 installed.
- The library Lib-Logger-0.7.0.jar.
- Included is the log4j-api-2.11.2.jar.
- Included is the log4j-core-2.11.2.jar.
- If not installed download the JRE 8 or the JDK 8.
- Optional: To work better with FXML files in a JavaFX application download the JavaFX Scene Builder under 'Additional Resources'.
- Choose your preferred IDE (e.g. NetBeans, Eclipse or IntelliJ IDEA) for development.
- Download or clone the library Lib-Logger.
- If you find a
Bug
I will be glad if you could report an Issue. - If you want to contribute to the project plz fork the project and do a Pull Request.
The project Lib-Logger
is licensed under General Public License 3.0.
The project Lib-Logger
is maintained by me, Peter Rogge. See Contact.
You can reach me under peter.rogge@yahoo.de.