Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging without a logging library

Here is the "problem". Every single library I develop logs something, however I don't want to require a library user to know that and to install log4net or similar logging implementation I would use. How are smart people creating standalone assemblies?

Logging output is not important for a library user unless he or she really wants to see the debug/info output. In this case he would install log4net and tell the library to use it somehow via the configuration file. Otherwise I'd like to ship my library without any irrelevant dependencies. Is this possible? What are the options?

In C++ I would simply create a couple of dll/so (with and without logging) statically linking them with a required library set, and if someone needs a library with logging enabled, he would simply overwrite it with another version.

like image 301
Ivan G. Avatar asked Apr 06 '11 00:04

Ivan G.


People also ask

What is a logging library?

A logging library (or logging framework) is code that you embed into your application to create and manage log events. Logging libraries provide APIs for creating, structuring, formatting, and transmitting log events in a consistent way. Like agents, they're used to send events from your application to a destination.

Is logging a default Python library?

Python provides a logging system as a part of its standard library, so you can quickly add logging to your application.

Is logging a standard library?

Logging in Python is simple and well-standardized, thanks to a powerful logging framework right in the standard library. Modules should simply log everything to a logger instance for their module name. This makes it easy for the application to route log messages of different modules to different places, if necessary.

What are the advantages to using a logging library?

Advantages of Using LoggersChoice of contextual information which is crucial. Separate log files for different components. Log levels(like DEBUG, WARN, INFO) which can be very handy when you want to track the path followed by your program or log queries, etc.


1 Answers

Excellent idea; things like these are the details that make life easier for both the writer and consumer of a library!

DotNetOpenAuth does this

I know the DotNetOpenAuth library behaves the way you describe. It uses log4net, but only if it's present and configured by the consuming assembly. From its README.Bin.html:

log4net.dll If present and v1.2.10, will allow for recording log messages this library emits.

There's also an informative page on how to configure DotNetOpenAuth logging in your own app.

[EDIT] I've inspected their source code, and they dynamically check for the log4net assembly and load+wire it if present (in Log4NetLogger.cs & Logger.cs). As mentioned in your comment below, they need log4net during compilation because it is referenced in several places. The clever part is that because all log4net-specific code is skipped when the check fails, there will be no runtime problems when the log4net assembly is missing.

DotNetOpenAuth is an open source project, so you should be able to mimic their approach from browsing through their initialization code.

like image 79
Joseph Tanenbaum Avatar answered Sep 29 '22 06:09

Joseph Tanenbaum