Create a Logger

Introduction

When we are developing a new application, for sure, we need to create a Logger.

Log an info/warning/error is useful for the user but, of course, much more for the developer to investigate on an issue.
Or, maybe, because the application has to provide a report of an investigation (performance or comparison test are just two examples).

Sometimes the existing Logger is not enough for our purposes and, for this reason, it’s necessary to write our own custom logger.
So, let’s see how to implement it…

Note: The article will show the code in CSharp but the method can be applied also in other languages.

Note 2: Don’t worry. At the end of the article there is a link to the GitHub’s repository.


Good Practice

First of all, let’s try to use some good practice in order to implement the Logger in the best way.

  • Folders: Maintain the classes in the right folders is the first thing to do.

Example: Do we have to create an Helper class? Let’s create an Helpers folder that will contains all the Helpers’ classes.

  • Divide by Tasks: Do not create a generic class which manage different behaviors. Create a specific class for the specific behavior.

Example: Do we need many methods that return the result of query? Let’s create a DataProvider class (MyClassDataProvider) that contains all the methods we need. Of course, save the class in the right folder (DataProviders).

  • Interfaces: It’s always a good practice to create classes by implementing interfaces and use them to declare objects in other classes.

  • Design Pattern: Always a best approach to use some design pattern. In this article, I’ll use some of them…


What do I need?

Always ask yourself this question, when you have to implement from scratch a new project. In this case, we have to create a Logger class that log… what and where?

What: Of course… a message :D But there are at least three types of messages to log:

  • Info
  • Warning
  • Error

Where: Ok.. where we have to log the message? In the console? In a File? In the Database? So, let’s assume that we need to log in the Console and in a File.

With these answers, we can start to create our Logger class.

Implementation

Organization

First of all, all the files that we will create must be organized by folders.
The name of the folders is very important: they must contain the classes that perform a certain action.

For this project we need this kind of classes:

  • Factory
  • Helper
  • Model
  • Service

This list is the exact list of folders that we need.

Factory Class

Let’s start to create a folder, named Factory, and create a Factory1 class with the name LogBase

https://salvatorecattano.it/resources/images/LogBase_Factory.png

This class contains the methods that all the inherited classes must have!

Inherited classes

Ok.. where do we want to log the info?

I chose two simple options: in a File and inside the Console.
We need two classes, which inherit the base class (LogBase), and identify them as a service.
So, let’s create a folder named Services and create the two classes.

  1. ConsoleLogService
  2. FileLogService

https://salvatorecattano.it/resources/images/ConsoleLogService.png https://salvatorecattano.it/resources/images/FileLogService.png

Models

We need some constants to use inside our code, avoiding using strings or anything else.

For example, a Target to define which Log service to use and a Type to define the type of the log (INFO, ERR, WARN).

Usually, I create an unique class that collects all the enums…

https://salvatorecattano.it/resources/images/Models.png

Helpers

It’s time to create the Helper that will “help" us to log the message.
Usually an helper’s class is declared as static… and this is the case for us.
This class use an object declared with the abstract class (LogBase) and initialized with defined target.

The static Log method is used to log the message with a specific service (using the enum LogTarget).

https://salvatorecattano.it/resources/images/LogHelper.png

We can avoid to send, in the Log method’s parameters, the LogType creating the dedicated methods.

For this reason, I proprose a variation of the Helpers class.

https://salvatorecattano.it/resources/images/LogHelper2.png

GitHub

You can find all the sources in my GitHub’s repository.


Updates

8 Dec 22’ :

  • Fixed possible bug in the FileLogService’s constructor
  • Small changes in the article