001package org.javasimon.utils;
002
003import org.javasimon.Split;
004import org.javasimon.StopwatchSample;
005import org.javasimon.callback.CallbackSkeleton;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008import org.slf4j.Marker;
009import org.slf4j.MarkerFactory;
010
011/**
012 * LoggingCallback logs events via SLF4J logging.
013 *
014 * @author <a href="mailto:virgo47@gmail.com">Richard "Virgo" Richter</a>
015 * @author nigel.thomas@york.ac.uk
016 * @since 3.0
017 */
018@SuppressWarnings("UnusedDeclaration")
019public class SLF4JLoggingCallback extends CallbackSkeleton {
020
021        private Logger logger = LoggerFactory.getLogger(SLF4JLoggingCallback.class);
022        private Marker marker = MarkerFactory.getMarker("FINE");
023
024        /**
025         * Logs Simon start on a specified log marker.
026         *
027         * @param split started split
028         */
029        @Override
030        public void onStopwatchStart(Split split) {
031                logger.debug(marker, "SIMON START: {}", split.getStopwatch());
032        }
033
034        /**
035         * Logs Simon stop on a specified log marker.
036         *
037         * @param split stopped split
038         * @param sample stopwatch sample
039         */
040        @Override
041        public void onStopwatchStop(Split split, StopwatchSample sample) {
042                logger.debug(marker, "SIMON STOP: {} ({})", sample.toString(), split.runningFor());
043        }
044
045        /**
046         * Logs the warning on a specified log marker.
047         *
048         * @param warning warning message
049         * @param cause throwable cause
050         */
051        @Override
052        public void onManagerWarning(String warning, Exception cause) {
053                logger.debug(marker, "SIMON WARNING: {}", warning, cause);
054        }
055
056        /**
057         * Logs the message on a specified log marker.
058         *
059         * @param message message
060         */
061        @Override
062        public void onManagerMessage(String message) {
063                logger.debug(marker, "SIMON MESSAGE: {}", message);
064        }
065
066        /**
067         * Returns logger used to log messages.
068         *
069         * @return used logger
070         */
071        public Logger getLogger() {
072                return logger;
073        }
074
075        /**
076         * Sets the logger that will be used to log messages.
077         *
078         * @param logger new specified logger
079         */
080        public void setLogger(Logger logger) {
081                this.logger = logger;
082        }
083
084        /**
085         * Returns the specified log marker for messages.
086         *
087         * @return log marker for messages
088         */
089        public Marker getMarker() {
090                return marker;
091        }
092
093        /**
094         * Sets the marker that will be used to log messages.
095         *
096         * @param marker log marker used to log messages
097         */
098        public void setMarker(Marker marker) {
099                this.marker = marker;
100        }
101
102        /**
103         * Sets the marker via marker name - used by the configure facility to configure the callback.
104         *
105         * @param marker name of the marker
106         */
107        public void setMarker(String marker) {
108                this.marker = MarkerFactory.getMarker(marker);
109        }
110}