Skip to content

Commit

Permalink
Configuring ZlgHandler via a properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoutanov committed Apr 27, 2018
1 parent 56b4ee2 commit 2ecebdb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
28 changes: 25 additions & 3 deletions bridge-jul/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ compile "com.obsidiandynamics.zerolog:zerolog-core:x.y.z"
compile "com.obsidiandynamics.zerolog:zerolog-bridge-jul:x.y.z"
```

## Sample code
The following example installs the bridge (stashing prior handlers first) and demonstrates logging via JUL, ending up in Zlg. Afterwards the original handlers are restored from the stash.
## Installing programmatically
The following example installs the bridge for the root logger (stashing prior handlers first) and demonstrates logging via JUL, ending up in Zlg. Afterwards the original handlers are restored from the stash.

```java
final Logger logger = Logger.getLogger(JulZlgBridgeSample.class.getName());
Expand All @@ -40,4 +40,26 @@ JulZlgBridge.unstashAllHandlers();
logger.log(Level.INFO, "Goodbye");
```

You can keep the original handlers and use the bridge at the same time by skipping the (un)stashing.
You can keep the original handlers and use the bridge at the same time by skipping the (un)stashing steps.

**Note:** JUL will apply its own level-based filtering to the log entries prior to delegating them to the handler(s). By default, the root JUL logger operates at `Level.INFO`. To programmatically alter the level, invoke `setLevel(Level)` on the `Logger` instance.

## Installing via a properties file
The following sample properties file configures the `ZlgHandler` directly and enables all levels on the root logger.

```
handlers=com.obsidiandynamics.zerolog.ZlgHandler
.level=ALL
```

Configure by setting the `java.util.logging.config.file` system property (programmatically, or via a `-D...` JVM argument).

```java
System.setProperty("java.util.logging.config.file", "src/test/resources/jul.properties");

final Logger logger = Logger.getLogger(ZlgHandlerPropertiesSample.class.getName());

// logging will now end up with Zlg
logger.log(Level.INFO, "Pi is {0} ≈ {1}/{2}", new Object[] {Math.PI, 22, 7});
```

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
/**
* A {@link java.util.logging.Handler} wrapping a {@link Zlg} instance.
*/
final class ZlgHandler extends Handler {
public final class ZlgHandler extends Handler {
private static final String entrypoint = Logger.class.getName();

/** Used to format JUL messages. */
private static final SimpleFormatter simpleFormatter = new SimpleFormatter();

private final Zlg zlg;

public ZlgHandler() {
this(Zlg.forName("").get());
}

ZlgHandler(Zlg zlg) {
this.zlg = zlg;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public void testPublish() {
target.entries().assertCount(1);
target.entries().forLevel(LogLevel.DEBUG).withMessage("Pi is 3.14").assertCount(1);
}

@Test
public void testDefaultConstructor() {
final ZlgHandler handler = new ZlgHandler();
handler.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.obsidiandynamics.zerolog.sample;

import java.util.logging.*;

public final class ZlgHandlerPropertiesSample {
public static void main(String[] args) {
// configure ZlgHandler through a properties file
System.setProperty("java.util.logging.config.file", "src/test/resources/jul.properties");

final Logger logger = Logger.getLogger(ZlgHandlerPropertiesSample.class.getName());

// logging will now end up with Zlg
logger.log(Level.INFO, "Pi is {0} ≈ {1}/{2}", new Object[] {Math.PI, 22, 7});
}
}
2 changes: 2 additions & 0 deletions bridge-jul/src/test/resources/jul.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
handlers=com.obsidiandynamics.zerolog.ZlgHandler
.level=ALL

0 comments on commit 2ecebdb

Please sign in to comment.