Skip to content

Commit

Permalink
Fixed testing provider constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlalis committed Mar 1, 2023
1 parent d0db644 commit 4f20086
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
32 changes: 16 additions & 16 deletions source/slf4d/handler.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* The handler module defines the `LogHandler` interface, as well as several
* simple handler implementations that can be used or extended by SLF4D
* providers.
Expand All @@ -8,7 +8,7 @@ module slf4d.handler;
import slf4d.logger : LogMessage;
import slf4d.level;

/**
/**
* The interface for any component that consumes log messages generated by a
* `Logger`. Only messages whose level is greater than or equal to the logger's
* level will be sent to handlers. For example, a Logger configured at an INFO
Expand All @@ -31,7 +31,7 @@ shared interface LogHandler {
shared void handle(LogMessage msg);
}

/**
/**
* A log handler that discards all messages. Useful for testing.
*/
class DiscardingLogHandler : LogHandler {
Expand All @@ -40,17 +40,17 @@ class DiscardingLogHandler : LogHandler {
}
}

/**
/**
* A log handler that simply appends all messages it receives to an internal
* array. This can be useful for testing.
*/
synchronized class CachingLogHandler : LogHandler {
/**
/**
* The internal cache of messages.
*/
private shared LogMessage[] messages;

/**
/**
* "Handles" a log message by appending it to this handler's internal list
* of messages, for later inspection.
* Params:
Expand All @@ -60,14 +60,14 @@ synchronized class CachingLogHandler : LogHandler {
this.messages ~= msg;
}

/**
/**
* Resets this handler's internal message cache to an empty list.
*/
public shared void reset() {
this.messages = [];
}

/**
/**
* Gets an immutable duplication of the list of messages this handler has
* handled since the last `reset()` call.
* Returns: The list of messages.
Expand All @@ -76,7 +76,7 @@ synchronized class CachingLogHandler : LogHandler {
return cast(LogMessage[]) messages.idup;
}

/**
/**
* Gets the number of messages that this handler has handled since the last
* `reset()` call.
* Returns: The number of messages.
Expand All @@ -85,7 +85,7 @@ synchronized class CachingLogHandler : LogHandler {
return messages.length;
}

/**
/**
* Determines if no messages have been logged since the last time `reset()`
* was called.
* Returns: True if there are no messages, or false otherwise.
Expand All @@ -104,7 +104,7 @@ unittest {
assert(handler.getMessages().length == 1);
}

/**
/**
* A log handler that simply passes any log message it receives to a list of
* other handlers. Note that handlers should only be added at application
* startup, because the `handle` method is not synchronized to improve
Expand All @@ -113,7 +113,7 @@ unittest {
class MultiLogHandler : LogHandler {
private shared LogHandler[] handlers;

/**
/**
* Constructs this multi-log handler using the given list of sub-handlers.
* Params:
* handlers = The handlers that should each handle every message this
Expand All @@ -123,7 +123,7 @@ class MultiLogHandler : LogHandler {
this.handlers = handlers;
}

/**
/**
* Adds a handler to this multi-handler's list of handlers.
* Params:
* handler = The handler to add.
Expand All @@ -134,7 +134,7 @@ class MultiLogHandler : LogHandler {
return this;
}

/**
/**
* Handles a log message by calling each of its sub-handlers' `handle`
* methods on it.
* Params:
Expand All @@ -158,7 +158,7 @@ unittest {
assert(h2.getMessages().length == 1);
}

/**
/**
* A handler that applies a filter to log messages, and only passes messages to
* its internal handler if the filter returns `true`.
*/
Expand Down Expand Up @@ -194,7 +194,7 @@ unittest {
assert(baseHandler.getMessages().length == 1);
}

/**
/**
* A handler that sends log messages to different handlers depending on the
* level of the message. For example, you could create a level-mapped log
* handler that sends INFO messages to stdout, but sends ERROR messages to
Expand Down
17 changes: 15 additions & 2 deletions source/slf4d/testing_provider.d
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* An SLF4D provider that is intended to be used for testing. The provider
* class, `TestingLoggingProvider`, includes some convenience methods to
* inspect the log messages generated while it's active.
*/
module slf4d.testing_provider;

import slf4d.factory;
Expand All @@ -15,7 +20,11 @@ class TestingLoggingProvider : LoggingProvider {
/**
* The logger factory that this provider uses.
*/
public shared TestingLoggerFactory factory = new TestingLoggerFactory();
public shared TestingLoggerFactory factory;

public shared this() {
this.factory = new shared TestingLoggerFactory();
}

public shared shared(TestingLoggerFactory) getLoggerFactory() {
return this.factory;
Expand Down Expand Up @@ -43,9 +52,13 @@ class TestingLoggingProvider : LoggingProvider {
* manner.
*/
class TestingLoggerFactory : LoggerFactory {
public shared CachingLogHandler handler = new shared CachingLogHandler();
public shared CachingLogHandler handler;
public Level logLevel = Levels.TRACE;

public shared this() {
this.handler = new shared CachingLogHandler();
}

shared Logger getLogger(string name = __MODULE__) {
return Logger(this.handler, this.logLevel, name);
}
Expand Down

0 comments on commit 4f20086

Please sign in to comment.