SharpC
SharpC

Reputation: 7454

Important but non-fatal log messages in logging frameworks (log4j, log4net, log4php, log4cxx, NLog)

In certain applications, there are important messages you always want to log (such as the application starting up and shutting down) regardless of the current log level setting. The only way to ensure an important message is logged using frameworks such as log4j and log4net is to set the log level of the message raised as FATAL.

Is there a better way to do this, as FATAL seems a misleading category when the application starts up fine with no errors?

Upvotes: 2

Views: 1007

Answers (3)

Sven
Sven

Reputation: 70863

No, setting message logging to the highest available logging level just to make sure they are always logged is the wrong approach.

First of all, define what you want to have. For example, why is it important to log the starting and stopping of the application?

Note that there are multiple kinds of logging. The usual logging is some kind of help for the developer when investigating what's going wrong. This is debugging logging, and it usually is optional, because it doesn't really matters for the logging part if the application works or not. Debugging can be done by different means if available.

Another kind of logging is audit logging. There might be requirements to log if and when a user logs in or out, changes passwords or does anything else that is of interest at a later time, to be able to verify every action was correctly initiated and completed.

Any of the Log4X frameworks should be able to support both logging requirements, but the difference is that you should configure them differently. The audit logging does not allow any message not to be sent to the logging appender, but it is very likely the appenders are different than those of the debug logging. Take advantage of the fact that you will have multiple loggers and can configure them differently.

Upvotes: 0

Andreas Fester
Andreas Fester

Reputation: 36630

For log4j (and, similarly for log4cxx), you could do something like this:

public class Always {
  private static Logger logger = Logger.getLogger("logAlways");

  public static void log(String message) {
      logger.info(message);
  }
}

and then configure the logAlways logger to log everything above INFO:

log4j.logger.logAlways=INFO

This could also be configured through the API to avoid that someone (un)intentionally changes the configuration file:

public class Always {
  private static Logger logger = null;

  static {
     logger = Logger.getLogger("logAlways");
     logger.setLevel(Level.INFO);
  }

  public static void log(String message) {
      logger.info(message);
  }
}

Use it then like

Always.log("Starting up");

Upvotes: 2

Peter
Peter

Reputation: 27944

The log4net provides all kind of configuration on how to log and where to log. You can make filters to make and special appenders to certain messages to log some messages of the level info. The thing you want to do is allready possible in the log4X framework. Have a look at the documentation to see how to do this.

log4net manual

ILog log = LogManager.GetLogger("MyAllwaysLogMessages");
log.Info(....

Now you can filter on MyAllwaysLogMessages to add those to your log.

Upvotes: 1

Related Questions