| 1 | package com.github.markusbernhardt.proxy.util; |
| 2 | |
| 3 | /***************************************************************************** |
| 4 | * Simple logging support for the framework. You need to add an logging listener |
| 5 | * that needs to send the logging events to an backend. |
| 6 | * |
| 7 | * @author Markus Bernhardt, Copyright 2016 |
| 8 | * @author Bernd Rosstauscher, Copyright 2009 |
| 9 | ****************************************************************************/ |
| 10 | |
| 11 | public class Logger { |
| 12 | |
| 13 | public enum LogLevel { |
| 14 | ERROR, WARNING, INFO, TRACE, DEBUG |
| 15 | } |
| 16 | |
| 17 | /***************************************************************************** |
| 18 | * Interface for an logging backend that can be attached to the logger. |
| 19 | ****************************************************************************/ |
| 20 | |
| 21 | public interface LogBackEnd { |
| 22 | |
| 23 | /************************************************************************* |
| 24 | * Invoked for every logging event. |
| 25 | * |
| 26 | * @param clazz |
| 27 | * the class that sends the log message. |
| 28 | * @param loglevel |
| 29 | * the logging level. |
| 30 | * @param msg |
| 31 | * the message format string. |
| 32 | * @param params |
| 33 | * the message parameters for the format string. |
| 34 | ************************************************************************/ |
| 35 | |
| 36 | public void log(Class<?> clazz, LogLevel loglevel, String msg, Object... params); |
| 37 | |
| 38 | /************************************************************************* |
| 39 | * Can be used to test if a given logging level is enabled. |
| 40 | * |
| 41 | * @param logLevel |
| 42 | * the loglevel to test. |
| 43 | * @return true if enabled, else false. |
| 44 | ************************************************************************/ |
| 45 | |
| 46 | public boolean isLogginEnabled(LogLevel logLevel); |
| 47 | } |
| 48 | |
| 49 | private static LogBackEnd backend; |
| 50 | |
| 51 | /************************************************************************* |
| 52 | * Gets the currently attached logging backend. |
| 53 | * |
| 54 | * @return Returns the backend. |
| 55 | ************************************************************************/ |
| 56 | |
| 57 | public static LogBackEnd getBackend() { |
| 58 | return backend; |
| 59 | } |
| 60 | |
| 61 | /************************************************************************* |
| 62 | * Attaches a new logging backend replacing the existing one. |
| 63 | * |
| 64 | * @param backend |
| 65 | * The backend to set. |
| 66 | ************************************************************************/ |
| 67 | |
| 68 | public static void setBackend(LogBackEnd backend) { |
| 69 | Logger.backend = backend; |
| 70 | } |
| 71 | |
| 72 | /************************************************************************* |
| 73 | * Logs a message. |
| 74 | * |
| 75 | * @param clazz |
| 76 | * the class that sends the log message. |
| 77 | * @param loglevel |
| 78 | * the logging level. |
| 79 | * @param msg |
| 80 | * the message format string. |
| 81 | * @param params |
| 82 | * the message parameters for the format string. |
| 83 | ************************************************************************/ |
| 84 | |
| 85 | public static void log(Class<?> clazz, LogLevel loglevel, String msg, Object... params) { |
| 86 | if (backend != null) { |
| 87 | backend.log(clazz, loglevel, msg, params); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /************************************************************************* |
| 92 | * Can be used to test if a given logging level is enabled. |
| 93 | * |
| 94 | * @param logLevel |
| 95 | * the loglevel to test. |
| 96 | * @return true if enabled, else false. |
| 97 | ************************************************************************/ |
| 98 | |
| 99 | public static boolean isLogginEnabled(LogLevel logLevel) { |
| 100 | if (backend != null) { |
| 101 | return backend.isLogginEnabled(logLevel); |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | } |