LoggerFactory

In a recently project, I needed a good log system, as usual I looking for any one, no found one, well, no that adjust to my needs, so I wrote LoggerFactory my own logger.

Should fit this (and others) needs:

  1. Easy to use
  2. Extensible
  3. Asynchronous by default using threads
  4. Various outputs by TypeLevel
  5. Custom format outputs

Based on very good loggers as java.util.logging or MacOS Logging, I made LoggerFactory the result was very thin with no more 900+ lines of code, with the features I want for my project and for any other projects what need it.

Example:

  Dim logger As LoggerFactory.Logger= LoggerFactory.Get(CurrentMethodName)
  '...
  logger.Info "this is a info"
  '...
  logger.Debug "this is a debug"
  '...
  logger.Trace "this is a trace"
  '...
  logger.Error "this is a error"
  '...
  logger.Warn "this is a warning"
  '...
  logger.Fatal "this is a fatal"

Levels: ALL, DEBUG, ERROR, FATAL, INFO, TRACE, WARN

By default console and system.DebugLog are set, you can configure adding a Adder with LoggerFactory.Adders.Append example:

  LoggerFactory.Adders.Append New LoggerFactory.ConsoleAdder(LoggerFactory.LevelType.ALL)
  LoggerFactory.Adders.Append New LoggerFactory.FileLogAdder(LoggerFactory.LevelType.ALL)

In the previous snippet, set one “Addler” for console and other for file, here I explain “Adders”, Adders is for “add” outputs (messages) for each level, or to a file, properties or file. The default Adders are:

  • ConsoleAdder: Outputs for console (only for console proyects)
  • SystemDebugLogAdder: The normal System.DebugLog
  • SystemLogAdder: The System.Log
  • FileLogAdder: Outputs to file

One intetesting thing is we can, for example, set DEBUG outputs to SystemDebugLogAdder, plus ERROR outputs to a file, change the output format also you made you own Adders, You can sub-classing LoggerFactory.Adder and overwrite “Log” method; You can create outputs to, for example, to a database or web service; only setting Adders, no other code change.

Another interesting feature of Adders is setting the output format, like:

  Dim addFile As New LoggerFactory.FileLogAdder(LoggerFactory.LevelType.ALL)
  addFile.Format= "%d"+ TAB+ "%t"+ TAB+ "%l"+ TAB+ "%c"+ TAB+ "[%i]"+ TAB+ "%m"
  '...

Tokens:

%d: Date
%t: Time
%l: Level
%c: Counter
%u: SubSystem
%a: Category
%i: ID
%o: ObjectID
%e: EventType
%m: Message

“SEP” is a property of adder, TAB [Chr(9) en ASCII] is default and you can also set.
The FileLogAdder can be set “roll over”, with the FileLogAdder.FileType property and could be: same file, yearly, monthly, daily, hourly or by size.

Last, I talk about “Formatter”, you can use Str() o Format() for format outputs or code as:

  Dim n1 As UInt64= 202002021101
  Dim n2 As Double= 12.3456
  Dim n3 As Single= -12345.6789
  Dim s1 As String= "world"
  logger.Debug "i = %2d, n1= %u, n2= %3.5f, hello %s, single= %-###,##0.0###f - %{pub}s", _
  i, n1, n2, s1, n3, "test"

Tokens:

%d: Signed integers
%u: Unsigned integers
%f: Signed doubles/singles
%s: Strings

Others features are Sub-Systems and nanoseconds precision time, but that is for other post, If you think this is interesting or have questions, please commet it. So long.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s