fix: unix socket peercred on FreeBSD
[lttng-ust.git] / doc / examples / java-log4j / Hello.java
CommitLineData
501f6777 1/*
d60dfbe4 2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
501f6777
CB
3 * Copyright (C) 2014 - Christian Babeux <christian.babeux@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
501f6777 23
d60dfbe4
AM
24import java.io.IOException;
25
26import org.apache.log4j.Appender;
501f6777 27import org.apache.log4j.BasicConfigurator;
8685da11 28import org.apache.log4j.Logger;
ba5b4491 29import org.apache.log4j.Level;
d60dfbe4 30import org.lttng.ust.agent.log4j.LttngLogAppender;
501f6777 31
5bfeaeca 32/**
1820ea46
AM
33 * Example application using the LTTng-UST Java log4j agent.
34 *
35 * <p>
36 * To obtain LTTng trace events, you should run the following sequence of
37 * commands:
38 * </p>
39 *
40 * <ul>
41 * <li>$ lttng create</li>
42 * <li>$ lttng enable-event -l -a</li>
43 * <li>$ lttng start</li>
44 * <li>(run this program)</li>
45 * <li>$ lttng stop</li>
46 * <li>$ lttng view</li>
47 * <li>$ lttng destroy</li>
48 * </ul>
5bfeaeca 49 *
d60dfbe4 50 * @author Alexandre Montplaisir
5bfeaeca
AM
51 * @author Christian Babeux
52 */
53public class Hello {
54
d60dfbe4 55 private static final Logger HELLO_LOG = Logger.getLogger(Hello.class);
501f6777 56
5bfeaeca
AM
57 /**
58 * Application start
59 *
60 * @param args
61 * Command-line arguments
d60dfbe4 62 * @throws IOException
1820ea46
AM
63 * If the required native libraries cannot be found. You may
64 * have to specify "-Djava.library.path=..." on the "java"
65 * command line.
5bfeaeca 66 */
1820ea46 67 public static void main(String args[]) throws IOException {
ba5b4491
JR
68
69 /*
70 * Set lowest level to make sure all event levels are logged.
71 * Any jar can override the default log4j rootLogger level
72 * and a logger with no explicit level defaults to the non-null
73 * parent level. Events could be ignored if the inherited value
74 * is to low.
75 * e.g BSF -> https://issues.apache.org/jira/browse/BSF-24
76 */
77 HELLO_LOG.setLevel(Level.ALL);
78
d60dfbe4 79 /* Start with the default Log4j configuration, which logs to console */
501f6777 80 BasicConfigurator.configure();
d60dfbe4
AM
81
82 /*
1820ea46
AM
83 * Instantiate a LTTng log appender and attach it to the logger, which
84 * will now send the logged events to UST.
d60dfbe4
AM
85 */
86 Appender lttngAppender = new LttngLogAppender();
87 HELLO_LOG.addAppender(lttngAppender);
88
89 /*
90 * Here we've set up the appender programmatically, but it could also be
91 * defined at runtime, by reading a configuration file for example:
92 */
93 // PropertyConfigurator.configure(fileName);
501f6777 94
1820ea46 95 /* Trigger some tracing events using the Log4j Logger created before. */
d60dfbe4 96 HELLO_LOG.info("Hello World, the answer is " + 42);
1820ea46
AM
97 HELLO_LOG.info("Another info event");
98 HELLO_LOG.error("An error event");
501f6777 99
1820ea46
AM
100 /* Cleanup */
101 HELLO_LOG.removeAppender(lttngAppender);
d60dfbe4 102 lttngAppender.close();
501f6777
CB
103 }
104}
This page took 0.033354 seconds and 4 git commands to generate.