summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/util/Logger.java
blob: f434699fdab01cc05b8604c4b56e955a10ce493c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.btr.proxy.util;

import java.text.MessageFormat;

/*****************************************************************************
 * Simple logging support for the framework.
 * You need to add an logging listener that needs to send the logging events
 * to an backend.
 *
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class Logger {
	
	public enum LogLevel {ERROR, WARNING, INFO, TRACE, DEBUG}

	/*****************************************************************************
	 * Interface for an logging backend that can be attached to the logger.
	 ****************************************************************************/
	
	public interface LogBackEnd {
		
		/*************************************************************************
		 * Invoked for every logging event.
		 * @param clazz the class that sends the log message.
		 * @param loglevel the logging level.
		 * @param msg the message format string.
		 * @param params the message parameters for the format string.
		 ************************************************************************/
		
		public void log(Class<?> clazz, LogLevel loglevel, String msg, Object ...params);

		/*************************************************************************
		 * Can be used to test if a given logging level is enabled.  
		 * @param logLevel the loglevel to test.
		 * @return true if enabled, else false.
		 ************************************************************************/
		
		public boolean isLogginEnabled(LogLevel logLevel);
	}
	
	private static LogBackEnd backend;
	
	/*************************************************************************
	 * Gets the currently attached logging backend.
	 * @return Returns the backend.
	 ************************************************************************/
	
	public static LogBackEnd getBackend() {
		return backend;
	}

	/*************************************************************************
	 * Attaches a new logging backend replacing the existing one.
	 * @param backend The backend to set.
	 ************************************************************************/
	
	public static void setBackend(LogBackEnd backend) {
		Logger.backend = backend;
	}

	/*************************************************************************
	 * Logs a message.
	 * @param clazz the class that sends the log message.
	 * @param loglevel the logging level.
	 * @param msg the message format string.
	 * @param params the message parameters for the format string.
	 ************************************************************************/
	
	public static void log(Class<?> clazz, LogLevel loglevel, String msg, Object ...params) {
		System.out.println(MessageFormat.format(msg, params));
	}
	
	/*************************************************************************
	 * Can be used to test if a given logging level is enabled.  
	 * @param logLevel the loglevel to test.
	 * @return true if enabled, else false.
	 ************************************************************************/
	
	public static boolean isLogginEnabled(LogLevel logLevel) {
		if (backend != null) {
			return backend.isLogginEnabled(logLevel);
		}
		return false;
	}

}