Move to kernel style SPDX license identifiers
[lttng-ust.git] / doc / examples / java-log4j / Hello.java
CommitLineData
501f6777 1/*
c0c0989a 2 * SPDX-License-Identifier: MIT
501f6777 3 *
c0c0989a
MJ
4 * Copyright (C) 2015 EfficiOS Inc.
5 * Copyright (C) 2015 Alexandre Montplaisir <alexmonthy@efficios.com>
6 * Copyright (C) 2014 Christian Babeux <christian.babeux@efficios.com>
501f6777 7 */
501f6777 8
d60dfbe4
AM
9import java.io.IOException;
10
11import org.apache.log4j.Appender;
501f6777 12import org.apache.log4j.BasicConfigurator;
8685da11 13import org.apache.log4j.Logger;
ba5b4491 14import org.apache.log4j.Level;
d60dfbe4 15import org.lttng.ust.agent.log4j.LttngLogAppender;
501f6777 16
5bfeaeca 17/**
1820ea46
AM
18 * Example application using the LTTng-UST Java log4j agent.
19 *
20 * <p>
21 * To obtain LTTng trace events, you should run the following sequence of
22 * commands:
23 * </p>
24 *
25 * <ul>
26 * <li>$ lttng create</li>
27 * <li>$ lttng enable-event -l -a</li>
28 * <li>$ lttng start</li>
29 * <li>(run this program)</li>
30 * <li>$ lttng stop</li>
31 * <li>$ lttng view</li>
32 * <li>$ lttng destroy</li>
33 * </ul>
5bfeaeca 34 *
d60dfbe4 35 * @author Alexandre Montplaisir
5bfeaeca
AM
36 * @author Christian Babeux
37 */
38public class Hello {
39
d60dfbe4 40 private static final Logger HELLO_LOG = Logger.getLogger(Hello.class);
501f6777 41
5bfeaeca
AM
42 /**
43 * Application start
44 *
45 * @param args
46 * Command-line arguments
d60dfbe4 47 * @throws IOException
1820ea46
AM
48 * If the required native libraries cannot be found. You may
49 * have to specify "-Djava.library.path=..." on the "java"
50 * command line.
5bfeaeca 51 */
1820ea46 52 public static void main(String args[]) throws IOException {
ba5b4491
JR
53
54 /*
55 * Set lowest level to make sure all event levels are logged.
56 * Any jar can override the default log4j rootLogger level
57 * and a logger with no explicit level defaults to the non-null
58 * parent level. Events could be ignored if the inherited value
59 * is to low.
60 * e.g BSF -> https://issues.apache.org/jira/browse/BSF-24
61 */
62 HELLO_LOG.setLevel(Level.ALL);
63
d60dfbe4 64 /* Start with the default Log4j configuration, which logs to console */
501f6777 65 BasicConfigurator.configure();
d60dfbe4
AM
66
67 /*
1820ea46
AM
68 * Instantiate a LTTng log appender and attach it to the logger, which
69 * will now send the logged events to UST.
d60dfbe4
AM
70 */
71 Appender lttngAppender = new LttngLogAppender();
72 HELLO_LOG.addAppender(lttngAppender);
73
74 /*
75 * Here we've set up the appender programmatically, but it could also be
76 * defined at runtime, by reading a configuration file for example:
77 */
78 // PropertyConfigurator.configure(fileName);
501f6777 79
1820ea46 80 /* Trigger some tracing events using the Log4j Logger created before. */
d60dfbe4 81 HELLO_LOG.info("Hello World, the answer is " + 42);
1820ea46
AM
82 HELLO_LOG.info("Another info event");
83 HELLO_LOG.error("An error event");
501f6777 84
1820ea46
AM
85 /* Cleanup */
86 HELLO_LOG.removeAppender(lttngAppender);
d60dfbe4 87 lttngAppender.close();
501f6777
CB
88 }
89}
This page took 0.028732 seconds and 4 git commands to generate.