https://github.com/pwall567/log-front-test
Test functions for log-front logging library
https://github.com/pwall567/log-front-test
java library logging unit-testing
Last synced: 10 months ago
JSON representation
Test functions for log-front logging library
- Host: GitHub
- URL: https://github.com/pwall567/log-front-test
- Owner: pwall567
- License: mit
- Created: 2025-02-23T12:03:16.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-13T13:17:24.000Z (over 1 year ago)
- Last Synced: 2025-04-21T08:56:25.019Z (about 1 year ago)
- Topics: java, library, logging, unit-testing
- Language: Java
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# log-front-test
[](https://github.com/pwall567/log-front-test/actions/workflows/build.yml)
[](https://opensource.org/licenses/MIT)
[](https://central.sonatype.com/artifact/io.jstuff/log-front-test)
Testing functions for the [`log-front`](https://github.com/pwall567/log-front) logging library.
## Background
When testing an application that uses logging, a common requirement is to confirm that logging messages were output as
expected.
This library provides a simple mechanism for testing log output from applications that use the
[`log-front`](https://github.com/pwall567/log-front) logging library.
## Quick Start
The `LogList` class stores a list of all or selected logging messages output using `log-front`.
For example:
```java
try (LogList logList = new LogList()) {
Logger log = Log.getLogger("xxx");
// code that outputs log message to "log"
assertTrue(logList.hasInfo("Account created"));
}
```
`LogList` makes use of the `LogListener` mechanism in `log-front`.
Because `LogListener` implements `AutoCloseable`, the easiest way of using it is within a try-with-resources block; that
way the `LogList` will exist (and be supplied with log events) only for the duration of the test code.
## Reference
### `LogList`
The simplest form of `LogList` intercepts log events from all `Logger` instances:
```java
LogList logList = new LogList();
```
But sometimes it can be preferable to intercept log events for a selected set of `Logger`s; perhaps just one.
A single `Logger` may be identified by name:
```java
LogList logList = new LogList("xxx");
```
Or by class:
```java
LogList logList = new LogList(AccountsService.class);
```
Or for more complex cases, a [`StringMatcher`](https://github.com/pwall567/string-matcher) may be supplied:
```java
LogList logList = new LogList(StringMatcher.wildcard("Accounts*"));
```
The `LogList` implements `List` (with all modifying operations returning `UnsupportedOperationException`), so
it is possible to iterate through the list to locate a particular item.
But the more convenient way of examining the log output is to use one of the `hasXxxx()` functions:
| Name | Argument | Returns `true` if the list contains... |
|----------------------|----------|------------------------------------------------------------------|
| `hasTrace` | `Object` | ...a TRACE event with the specified message object |
| `hasDebug` | `Object` | ...a DEBUG event with the specified message object |
| `hasInfo` | `Object` | ...an INFO event with the specified message object |
| `hasWarn` | `Object` | ...a WARN event with the specified message object |
| `hasError` | `Object` | ...an ERROR event with the specified message object |
| `hasTraceContaining` | `String` | ...a TRACE event with a message containing the specified string |
| `hasDebugContaining` | `String` | ...a DEBUG event with a message containing the specified string |
| `hasInfoContaining` | `String` | ...an INFO event with a message containing the specified string |
| `hasWarnContaining` | `String` | ...a WARN event with a message containing the specified string |
| `hasErrorContaining` | `String` | ...an ERROR event with a message containing the specified string |
The `hasXxxx()` functions perform an exact match on the message object, but it is often simpler to compare just a
substring of the message.
The `hasXxxxContaining()` functions perform this task.
### `LogItem`
For those needing to perform more detailed checks on the log entries, the `LogList` stores log events as `LogItem`s,
which contain the following:
| Name | Type | Description |
|-------------|-------------|-------------------------------------------------------|
| `time` | `long` | the time in milliseconds from the standard epoch |
| `name` | `String` | the `Logger` name |
| `level` | `Level` | the level of the logging event |
| `message` | `Object` | the log message |
| `throwable` | `Throwable` | the `Throwable` associated with the log event, if any |
All of the logging functions take a message in the form of an `Object`; this allows an alternative means of lazy
creation of the message string (using the `toString()` on the object) – for example, supplying a JSON object as
the message.
The `Object` form is provided in the `LogItem`, allowing the consumer of the `LogItem` to take advantage of any inherent
structure in the message.
A convenience method gets the string form of the message (`null` will be returned as an empty string):
- `getMessageString()`
In addition to the usual `toString()` method, `LogItem` has three overloaded versions:
- `toString(char separator)`
- `toString(ZoneId zoneId)`
- `toString(char separator, ZoneId zoneId)`
The `toString()` is intended mainly for debugging, and these additional functions allow the separator (the default is
space) and the `ZoneId` to be used when formatting the time (the default is the current default time zone) to be
specified.
## Dependency Specification
The latest version of the library is 6.2 (the version number of this library matches the version of `log-front` to which
it relates), and it may be obtained from the Maven Central repository.
(The following dependency declarations assume that the library will be included for test purposes; this is expected to
be its principal use.)
### Maven
```xml
io.jstuff
log-front-test
6.2
test
```
### Gradle
```groovy
testImplementation 'io.jstuff:log-front-test:6.2'
```
### Gradle (kts)
```kotlin
testImplementation("io.jstuff:log-front-test:6.2")
```
Peter Wall
2025-03-13