Hey folks,

For the past couple of days I took the liberty of partially
implementing a logging module for D. I say partially because all the
features that I want to implement are not currently implement. You
should really look at the implementation more as a proof of concept
even thought most of the code will be used in the final
implementation.

That been said I am really interested in getting some feedback on the
API. That includes high-level design (e.g. using a thread to perform
logging. On that note I am planning to also have a shared memory
implementation), interfaces and the documentation.

When making comment be aware that the design goals of the module are:
1) Provide a logging mechanism that is easy to use in the common case.
2) The module should allow for as much configuration as possible at
compile time and execution time without breaking design goal 1.
3) It should be possible to extend or replace the backend without
breaking the semantic exposed by the API.

I am fairly new to the D language so any comment on how I can take
advantage of D idiom or D features in the API or implementation would
be greatly appreciated.

My intent, and hopefully we will get there with your help, is to
include this in Phobos for D2.

Thanks,
-Jose

On Mon, Apr 25, 2011 at 12:03 AM, Andrei Alexandrescu
<seewebsiteforem...@erdani.org> wrote:
> On 04/24/2011 02:23 PM, Sean Cavanaugh wrote:
>>
>> On 4/20/2011 11:09 AM, Robert Clipsham wrote:
>>>
>>> Hey folks,
>>>
>>> I've just finished porting my web framework from D1/Tango to D2/Phobos,
>>> and in the transition lost logging functionality. As I'll be writing a
>>> logging library anyway, I wondered if there'd be interest in a std.log?
>>> If so, is there a current logging library we would like it to be based
>>> on, or should we design from scratch?
>>>
>>> I know there has been discussion about Google's
>>> http://google-glog.googlecode.com/svn/trunk/doc/glog.html and another
>>> candidate may be http://logging.apache.org/log4j/ . Do we want a
>>> comprehensive logging library, or just the basics? (Possibly with some
>>> method for extension if needed).
>>>
>>
>>
>> I just wanted to mention Pantheios as a C++ logging system to take look
>> at as well, I didn't see it mentioned in this thread and it seems to
>> have all the major requirements for frontend/backend chaining and
>> whatnot that people have brought up. The code is on sourceforge to boot.
>
> I think Pantheios is an example of library design gone bad. It is
> fascinatingly overengineered.
>
> Andrei
>
>
Title: std.logging

std.logging

Implements application level logging mechanism.

This module defines a set of functions useful for many common logging tasks. The module must be initialized (ideally in single threaded mode) by calling initializeLogging. Messages of different severity level are logged by calling the template functions log and logf. Verbose messages can be logged by calling the template functions vlog and vlogf.

Examples:
import std.logging;

int main(string[] args)
{
   initializeLogging(ActorLogger.getCreator(args[0]));

   // ...
   logf(Level.info, "Log message from %s", args[0]);
}


Note:
Logging can be disable at compile time by defining the LOGGING_DISABLED version.

enum Level;
Defines the severity levels supported by the logging library.

Logging messages of severity level Level.fatal will also cause the program to halt. Messages of a given severity will be written in the log file of that severity and the log files of lower severity.

fatal
error
warning
info


void initializeLogging(shared(Logger) delegate() logCreator, LogConfig logConfig = LogConfig(null,cast(Level)1,null,null));
Initializes the logging infrastructure.

This function must be called ounce before calling any of the logging functions.

Params:
shared(Logger) delegate() logCreator Delegate which creates the Logger used by the module.
LogConfig logConfig Module configuration object.

See Also:
LogConfig

void logf(string file = __FILE__, int line = __LINE__, T...)(Level level, lazy T args);
Logs a formatted message.

Logs a formatted message if level is of higher or equal severity as that specified in the LogConfig object passed to initializeLogging.

The first parameter in args will be used as the format string. See std.format.formattedWrite for a description of the format string.

void log(string file = __FILE__, int line = __LINE__, T...)(Level level, lazy T args);
Logs a message

Logs a message if level is of higher or equal severity as that specified in the LogConfig object passed to initializeLogging.

void vlogf(string file = __FILE__, int line = __LINE__, T...)(uint level, lazy T args);
Logs a formatted verbose message

Logs a formatted verbose message if the file and level matches one of the entries specified in the vLogConfigs property of the LogConfig object passed to initializeLogging.

The first parameter in args will be used as the format string. See std.format.formattedWrite for a description of the format string.

void vlog(string file = __FILE__, int line = __LINE__, T...)(uint level, lazy T args);
Logs a verbose message

Logs a verbose message if the file and level matches one of the entries specified in the vLogConfigs property of the LogConfig object passed to initializeLogging.

struct LogConfig;
Configuration struct for the module.

This object must be used to configure the logging module at initialization. All the properties are optional and can be use to configure the framework's behavior.

void level(Level level);
Level to use for logging.

The logging framework will only log messages with a severity greater than or equal to the value of this property.

void vLogConfigs(VLogConfig[] vLogConfigs);
Verbose logging configuration.

Messages logged by using the template function vlog or vlogf will be filtered by comparing against each VLogConfig until a match is found. If no match is found the verbose message will not get logged.

See Also:
VLogConfig

void fatalHandler(void function() fatalHandler);
Function pointer for handling log message with a severity of fatal.

This function will be called by the thread trying to log a fatal message by using log or logf. The function fatalHandler should not return; otherwise the framework will assert(false).

struct VLogConfig;
Structure for configuring verbose logging.

This structure is used to control verbose logging on a per module basis. A verbose message with level x will get logged at severity level Level.info if there is a VLogConfig entry that matches to the source file and the verbose level of that entry is greater than or equal to x.

static VLogConfig[] create(string config);
Creates an array of VLogConfig based on a configuration string.

The format of the configuration string is as follow "[pattern]=[level],...", where [pattern] may contain any character allowed in a file name and [level] must be convertible to an positive integer (greater than or equal to zero). If [pattern] contains a '*' then it must be at the start or the end. If [pattern] ends with a '*' then it will match any source file name that starts with the rest of [pattern]. If [pattern] starts with a '*' then it will match any source file name that ends with a the rest of [pattern].

For every [pattern]=[level] in the configuration string a VLogConfig will be created and included in the returned array.

abstract interface Logger;
Extension point for the module.

abstract void log(Level level, string msg);
Logs a message.

The method is called by the logging module whenever it decides that a message should be logged. It is recommend that the implementation of this method doesn't perform any filtering based on level since at this point all configured filters were applied.

The method is allow to return immediately without persisting the message.

abstract void flush();
Flushes pending log operations.

The method is called by the logging framework whenever it requires that the persistence of all previous log messages. For example the method is called when the client logs a fatal message.

The method must not return until all pending log operations complete.

class ActorLogger: std.logging.Logger;
Implements an actor based logging backend.

Log messages are sent to a logging thread which is responsible for persisting log messages. Messages of a given severity will be written in the log file of that severity and in the log files of lower severity. The file names of the log files created will follow the following pattern "[name].log.[level].[time]". The string [name] is the parameter name passed to getCreator. The string [time] is the time when the logger was created. The string [level] is the severity of the log file. A file for severity level 'x' will contain all log messages of greater or equal severity.

static shared(Logger) delegate() getCreator(string name);
Returns a delegate for creating an ActorLogger.

The method will always return a different delegate but a given delegate will always return the same ActorLogger.

Params:
string name Name to use when creating log files


Page generated by Ddoc.

Attachment: logging.d
Description: Binary data

Reply via email to