slf4d 0.0.1

Simple Logging Facade For D


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

SLF4D

Simple Logging Facade For D, inspired by SLF4J.

This library provides a common framework for detailed application logging, while allowing end users to use a logging provider of their choice. SLF4D gives you a basic Logger that exposes various log methods, and a logging provider will handle log messages generated by your application in any way it likes. Providers may:

  • Format log messages however they like.
  • Distribute log messages to other systems (cloud logging systems, for example).
  • Perform additional filtering on logs.
  • And much more.

Different logging providers are hooked into the SLF4D framework with a logging adapter library.

The following diagram illustrates the SLF4D flow of information.

                    /----------------------------\
                    |  LoggerFactory (Provider)  |  <------+
                    \----------------------------/         |
                                |                          |
                                |   getLogger()            |
                               \ /                         |
                                v                          |
/----------------\ log("msg") /------------------\   /------------\
|  Application   | -------->  |  Logger (SLF4D)  |   |  Provider  |
\----------------/            \------------------/   \------------/
                                 |                         |
                                 |   handle(msg)           |
                                \ /                        |
                                 v                         |
                      /-------------------------\          |
                      |  LogHandler (Provider)  |  <-------+
                      \-------------------------/
  • Your application produces log messages using a Logger.
  • A Logger is obtained from a LoggerFactory that's supplied by a provider.
  • When you log a message, the message is handled by a LogHandler that's supplied by a provider.

Using SLF4D in your Application

Generally, in your application's main module, you'll define a static LoggerFactory instance that can be imported in all other modules:

module app_main;

import slf4d;
import slf4d_some_provider : getLoggerFactory();

static LoggerFactory loggerFactory;
static this() {
	loggerFactory = getLoggerFactory();
}

Then, in any module of your application, you can simply import the loggerFactory from your main module, and call getLogger() to get a Logger to use.

module some_other_module;

import app_main : loggerFactory;

void doStuff() {
    auto log = loggerFactory.getLogger();
    // Do things...
    log.info("This is my log message.");
}

Default Provider

In case you just want to get started using SLF4D right away, we've included the slf4d.default_provider module which defines a basic provider that just logs simple messages to stdout and stderr.

Your setup would look like this:

module app_main;

import slf4d;
import slf4d.default_provider : getLoggerFactory();

static LoggerFactory loggerFactory;
static this() {
	loggerFactory = getLoggerFactory();
}

Making an SLF4D-Compatible Provider

There is generally just one requirement of an SLF4D provider: define a public getLoggerFactory() method that returns an instance of LoggerFactory.

This LoggerFactory should implement the standard factory method:

Logger getLogger(string name = __MODULE__);

The provider's implementation will mostly involve giving every constructed Logger an instance of a custom LogHandler.

Authors:
  • Andrew Lalis
Dependencies:
none
Versions:
3.0.1 2024-Apr-03
3.0.0 2024-Jan-27
2.4.3 2023-Aug-24
2.4.2 2023-Jul-17
2.4.1 2023-Jul-02
Show all 21 versions
Download Stats:
  • 14 downloads today

  • 123 downloads this week

  • 486 downloads this month

  • 18849 downloads total

Score:
3.3
Short URL:
slf4d.dub.pm