diff --git a/src/site/markdown/logging.md b/src/site/markdown/logging.md new file mode 100644 index 0000000..0dc9adb --- /dev/null +++ b/src/site/markdown/logging.md @@ -0,0 +1,213 @@ +# Logging + +This library uses JUL (`java.util.logging`) for its debug logs. + +Here's how you can display those logs, depending on your logging library: + + + +## Usage with JUL + +`src/main/resources/logging.properties` + +```properties +handlers = java.util.logging.ConsoleHandler + +java.util.logging.ConsoleHandler.level = ALL + +.level = INFO +io.socket.level = FINE +``` + +`src/main/java/MyApp.java` + +```java +public class MyApp { + private static final Logger logger = Logger.getLogger("MyApp"); + + public static void main(String[] argz) throws Exception { + InputStream stream = MyApp.class.getResourceAsStream("logging.properties"); + LogManager.getLogManager().readConfiguration(stream); + + Socket socket = IO.socket(URI.create("https://example.com")); + + socket.on(Socket.EVENT_CONNECT, args -> logger.info("connected!")); + + socket.connect(); + } +} +``` + +Reference: https://docs.oracle.com/en/java/javase/17/core/java-logging-overview.html + +## Usage with Log4j2 + +`pom.xml` + +```xml + + + + + org.apache.logging.log4j + log4j-api + 2.18.0 + + + org.apache.logging.log4j + log4j-core + 2.18.0 + + + org.apache.logging.log4j + log4j-jul + 2.18.0 + + ... + + ... + +``` + +Maven repository: + +- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api +- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core +- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-jul + +`src/main/resources/log4j2.xml` + +```xml + + + + + + + + + + + + + + + + +``` + +`src/main/java/MyApp.java` + +Either by setting the `java.util.logging.manager` environment variable: + +```java +public class MyApp { + private static final Logger logger; + + static { + System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager"); + logger = LogManager.getLogger(MyApp.class); + } + + public static void main(String[] argz) throws Exception { + Socket socket = IO.socket(URI.create("https://example.com")); + + socket.on(Socket.EVENT_CONNECT, args -> logger.info("connected!")); + + socket.connect(); + } +} +``` + +Or with the `Log4jBridgeHandler` class: + +```java +public class MyApp { + private static final Logger logger = LogManager.getLogger(MyApp.class); + + public static void main(String[] argz) throws Exception { + Log4jBridgeHandler.install(true, "", true); + + Socket socket = IO.socket(URI.create("https://example.com")); + + socket.on(Socket.EVENT_CONNECT, args -> logger.info("connected!")); + + socket.connect(); + } +} +``` + +Reference: https://logging.apache.org/log4j/2.x/log4j-jul/index.html + +## Usage with Slf4j + logback + +`pom.xml` + +```xml + + + + + ch.qos.logback + logback-classic + 1.2.11 + + + org.slf4j + jul-to-slf4j + 1.7.36 + + ... + + ... + +``` + +Maven repository: + +- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic +- https://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j + +`src/main/resources/logback.xml` + +```xml + + + true + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + +``` + +`src/main/java/MyApp.java` + +```java +public class MyApp { + private static final Logger logger = LoggerFactory.getLogger(MyApp.class); + + static { + SLF4JBridgeHandler.install(); + } + + public static void main(String[] argz) throws Exception { + Socket socket = IO.socket(URI.create("https://example.com")); + + socket.on(Socket.EVENT_CONNECT, args -> logger.info("connected!")); + + socket.connect(); + } +} +``` + +Reference: https://www.slf4j.org/manual.html diff --git a/src/site/site.xml b/src/site/site.xml index ef9fa82..5d1d34a 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -30,6 +30,7 @@ +