Last modified by Frank Fock on 2024/05/25 20:32

Show last authors
1 SNMP4J provides its own log framework adapter that allows to plugin virtually any logging framework, including Java logging and Log4J.
2
3 == Enable Simple Logging ==
4
5 In the main class execute the following code before any SNMP4J code is executed statically or at runtime:
6
7 {{code language="java"}}
8 static {
9 LogFactory.setLogFactory(new ConsoleLogFactory());
10 ConsoleLogAdapter.setDebugEnabled(true);
11 }
12 {{/code}}
13
14 == Enable Log4J Logging ==
15
16 In the main class put:
17
18 {{code language="java"}}
19 static {
20 LogFactory.setLogFactory(new Log4jLogFactory());
21 org.apache.log4j.BasicConfigurator.configure();
22 LogFactory.getLogFactory().getRootLogger().setLogLevel(LogLevel.ALL);
23 }
24 {{/code}}
25
26
27 == Enable Java Logging ==
28
29 To enable Java logging two steps are necessary:
30
31 === Code ===
32
33 {{code language="java"}}
34 static {
35 LogFactory.setLogFactory(new JavaLogFactory());
36 // Optionally set log level on root logger:
37 LogFactory.getLogFactory().getRootLogger().setLogLevel(LogLevel.ALL);
38 }
39 {{/code}}
40
41 Runtime Configuration
42
43 For SNMP4J-CLT 2.1 you need to configure the log handlers and format at runtime. In SNMP4J and any other SNMP4J-* API you can do this with code of course.
44
45 Add the parameter ##-Djava.util.logging.config.file=<file>## to the Java command line:
46
47 {{code language="java"}}
48 java -Djava.util.logging.config.file=java_logging.properties ...
49 {{/code}}
50
51 **java_logging.properties Sample Configuration**
52
53 {{code language="properties"}}
54 handlers = java.util.logging.ConsoleHandler
55 java.util.logging.ConsoleHandler.level = ALL
56 java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
57 java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %5$s%6$s%n
58 {{/code}}