B4J Library LOGBack - Logging Framework

So this is a wrapper for the LOGBack - The Generic, Reliable, Fast & Flexible Logging Framework

https://logback.qos.ch/

Dependencies:
#AdditionalJar: slf4j-api-1.7.25
#AdditionalJar: logback-core-1.2.3
#AdditionalJar: logback-classic-1.2.3

logback-core and logback-classic can be downloaded from : https://logback.qos.ch/dist/
slf4j-api from : https://www.slf4j.org/download.html

There are 2 classes in this wrapper: LOGBackLoggerFactory and LOGBackLogger
LOGBackLoggerFactory - holds the methods for initialization of the logger and some constants.
LOGBackLogger - this is going to be used so that you can append logs from your app.

How to setup (my implementation):
In the Main module of your app declare a public variable of LOGBackLoggerFactory and a private variable of LOGBackLogger - this one should be added in each class of you application.
B4X:
Sub Process_Globals
    Public LoggerFactory As LOGBackLoggerFactory
    Private Logger As LOGBackLogger
End Sub

When the app starts first thing we want to do is configure the logger:
B4X:
Sub AppStart (Args() As String)
    Dim RootLogger As LOGBackLogger = LoggerFactory.GetLogger2(LoggerFactory.ROOT_LOGGER_NAME)
    RootLogger.StopAppender("console")
    RootLogger.DetachAppender("console")
    RootLogger.SetLevel(LoggerFactory.Level_INFO)
#If DEBUG
    RootLogger.AddConsoleAppender("consoleappender", "%date{HH:mm:ss.SSS} %level %message - %logger [%thread]%n")
#Else
    RootLogger.AddFileAppender("fileappender", "%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level %-36logger{36} - %message [%thread]%n", "logs/app.log", True, False)
#End If

    Logger = LoggerFactory.GetLogger

    Logger.Info("Application initialization started")

    ..............
End Sub
Let me explain a little. LOGBack uses an xml file for configuration and to keep the simplicity of B4X language we will not use a file instead we are going to configure the logger programatically.
So first thing when the app start we get the root logger - this is the main logger which other loggers inherit from. (1st line)
Next we are going to stop the default console output and remove it from the logger so we can configure our own way the templating and how the logger should output. (2nd and 3rd line)
We should set what level of logging we want to output. (OFF, ERROR, WARN, INFO, DEBUG, TRACE, ALL), you noticed I set the level to INFO this means that every message from info and above (WARN, ERROR) will be outputed.
Next I want when I run my app in DEBUG to show the logs in the Logs window from the IDE, when I run in release I want to output logs to a file, or you can do both.
And this is a small decent configuration ...

Now how to log messages:
B4X:
Logger.Info("Application initialization started")
This line of code does that and it will output in debug mode:
17:20:34.312 INFO Application initialization started - ro.mindful.test.main [main]
And in release:
2018-01-17 17:22:41.782 INFO ro.mindful.test.main - Application initialization started [main]

You should have a Global LoggerFactory decalred in you Main module or where you want so you can get (initialize) the loggers for each class/module.

Some of the LOGBackLogger are self explainatory while other include some links to the logback documentation where you can read more about them.

Tips:
- You can set different levels to each logger. Example: You have a class that gives you trouble set the level for that class to DEBUG or TRACE so that it appends those logs to the output.
- You can output some logger to other files.
- If you app is multithreaded (like web apps that use jServer) you should use AsyncAppender - be sure to read about them at least :)

I am using this logging framework mostly because of the logging levels and the ability to output from what level i want and individually for each of the loggers.

Questions are not welcome ... Just kidding :) I am no expert and I will try to help you if I can and know how. I just started using this 2 days ago o_O
 

Attachments

  • LOGBack.zip
    6.6 KB · Views: 301
Top