Blame doc/examples/java-jul/Hello.java

Packit c04fcb
/*
Packit c04fcb
 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
Packit c04fcb
 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit c04fcb
 * of this software and associated documentation files (the "Software"), to
Packit c04fcb
 * deal in the Software without restriction, including without limitation the
Packit c04fcb
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit c04fcb
 * sell copies of the Software, and to permit persons to whom the Software is
Packit c04fcb
 * furnished to do so, subject to the following conditions:
Packit c04fcb
 *
Packit c04fcb
 * The above copyright notice and this permission notice shall be included in
Packit c04fcb
 * all copies or substantial portions of the Software.
Packit c04fcb
 *
Packit c04fcb
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit c04fcb
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit c04fcb
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit c04fcb
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit c04fcb
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit c04fcb
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit c04fcb
 * IN THE SOFTWARE.
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
import java.io.IOException;
Packit c04fcb
import java.util.logging.Handler;
Packit c04fcb
import java.util.logging.Logger;
Packit c04fcb
Packit c04fcb
import org.lttng.ust.agent.jul.LttngLogHandler;
Packit c04fcb
Packit c04fcb
/**
Packit c04fcb
 * Example application using the LTTng-UST Java JUL agent.
Packit c04fcb
 *
Packit c04fcb
 * 

Packit c04fcb
 * Basically all that is required is to instantiate a {@link LttngLogHandler}
Packit c04fcb
 * and to attach it to a JUL {@link Logger}. Then use the Logger normally to log
Packit c04fcb
 * messages, which will be sent to UST as trace events.
Packit c04fcb
 * 

Packit c04fcb
 * 

Packit c04fcb
 * {@link Logger} names are used as event names on the UST side. This means that
Packit c04fcb
 * by enabling/disabling certain events in the tracing session, you are
Packit c04fcb
 * effectively enabling and disabling Loggers on the Java side. Note that this
Packit c04fcb
 * applies only to {@link LttngLogHandler}'s. If other handlers are attached to
Packit c04fcb
 * the Logger, those will continue logging events normally.
Packit c04fcb
 * 

Packit c04fcb
 *
Packit c04fcb
 * 

Packit c04fcb
 * To obtain LTTng trace events, you should run the following sequence of
Packit c04fcb
 * commands:
Packit c04fcb
 * 

Packit c04fcb
 *
Packit c04fcb
 * 
    Packit c04fcb
     * 
  • $ lttng create
  • Packit c04fcb
     * 
  • $ lttng enable-event -j -a
  • Packit c04fcb
     * 
  • $ lttng start
  • Packit c04fcb
     * 
  • (run this program)
  • Packit c04fcb
     * 
  • $ lttng stop
  • Packit c04fcb
     * 
  • $ lttng view
  • Packit c04fcb
     * 
  • $ lttng destroy
  • Packit c04fcb
     * 
    Packit c04fcb
     *
    Packit c04fcb
     * @author Alexandre Montplaisir
    Packit c04fcb
     * @author David Goulet
    Packit c04fcb
     */
    Packit c04fcb
    public class Hello {
    Packit c04fcb
    Packit c04fcb
    	/** Class-wide JUL logger object */
    Packit c04fcb
    	private static final Logger LOGGER = Logger.getLogger(Hello.class.getName());
    Packit c04fcb
    Packit c04fcb
    	/**
    Packit c04fcb
    	 * Application start
    Packit c04fcb
    	 *
    Packit c04fcb
    	 * @param args
    Packit c04fcb
    	 *            Command-line arguments
    Packit c04fcb
    	 * @throws IOException
    Packit c04fcb
    	 *             If the required native libraries cannot be found. You may
    Packit c04fcb
    	 *             have to specify "-Djava.library.path=..." on the "java"
    Packit c04fcb
    	 *             command line.
    Packit c04fcb
    	 */
    Packit c04fcb
    	public static void main(String args[]) throws IOException {
    Packit c04fcb
    Packit c04fcb
    		/* Instantiate a LTTngLogHandler object, and attach it to our logger */
    Packit c04fcb
    		Handler lttngHandler = new LttngLogHandler();
    Packit c04fcb
    		LOGGER.addHandler(lttngHandler);
    Packit c04fcb
    Packit c04fcb
    		/* Log events using the JUL Logger created before. */
    Packit c04fcb
    		LOGGER.info("Hello World, the answer is " + 42);
    Packit c04fcb
    		LOGGER.info("Another info event");
    Packit c04fcb
    		LOGGER.severe("A severe event");
    Packit c04fcb
    Packit c04fcb
    		/* Cleanup */
    Packit c04fcb
    		LOGGER.removeHandler(lttngHandler);
    Packit c04fcb
    		lttngHandler.close();
    Packit c04fcb
    	}
    Packit c04fcb
    }