Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c0a22a3e0f99da00ee80c61c679f2740c4699711 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2011, 2017 IBM Corporation and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.http.jetty.internal;

import org.eclipse.jetty.util.log.*;

// NOTE: This class simply allows us to override the StdErrLog built into jetty
public class EquinoxStdErrLog implements Logger {

	private static int DEBUG = 0;
	private static int INFO = 1;
	private static int WARN = 2;
	private static int ERROR = 3;
	private static int OFF = 4;

	private static volatile int threshold = WARN;
	private static EquinoxStdErrLog root;

	private final Logger realLogger;
	private final String localName;

	public static synchronized EquinoxStdErrLog getRootLogger() {
		if (root != null)
			return root;

		root = new EquinoxStdErrLog(null, null);
		return root;
	}

	public static void setThresholdLogger(String property) {
		threshold = parseThresholdProperty(property);
		// this is a hack to make sure the built-in jetty StdErrLog is not being used
		org.eclipse.jetty.util.log.Logger rootLogger = Log.getRootLogger();
		if (rootLogger == null || (rootLogger instanceof StdErrLog)) {
			// The built-in jetty StdErrLog is be used; replace with ours.
			Log.setLog(getRootLogger());
		}

	}

	private static int parseThresholdProperty(String property) {
		if (property == null)
			return WARN;

		if (property.equals("debug")) //$NON-NLS-1$
			return DEBUG;

		if (property.equals("info")) //$NON-NLS-1$
			return INFO;

		if (property.equals("warn")) //$NON-NLS-1$
			return WARN;

		if (property.equals("error")) //$NON-NLS-1$
			return ERROR;

		if (property.equals("none")) //$NON-NLS-1$
			return OFF;

		return WARN;
	}

	public EquinoxStdErrLog(String name, Logger realLogger) {
		this.localName = name;
		this.realLogger = realLogger == null ? new StdErrLog(name) : realLogger;
		if (threshold == DEBUG)
			this.realLogger.setDebugEnabled(true);
	}

	@Override
	public org.eclipse.jetty.util.log.Logger getLogger(String name) {
		if ((name == null && this.localName == null) || (name != null && name.equals(this.localName)))
			return this;
		return new EquinoxStdErrLog(name, realLogger.getLogger(name));
	}

	// debugSOO = slf4j.getMethod("debug", new Class[]{String.class,Object.class,Object.class});
	@Override
	public void debug(String msg, Object... arg0) {
		if (threshold > DEBUG)
			return;

		realLogger.debug(msg, arg0);
	}

	// debugST = slf4j.getMethod("debug", new Class[]{String.class,Throwable.class});
	@Override
	public void debug(String msg, Throwable th) {
		if (threshold > DEBUG)
			return;

		realLogger.debug(msg, th);
	}

	// infoSOO = slf4j.getMethod("info", new Class[]{String.class,Object.class,Object.class});
	@Override
	public void info(String msg, Object... arg0) {
		if (threshold > INFO)
			return;

		realLogger.info(msg, arg0);
	}

	// warnSOO = slf4j.getMethod("warn", new Class[]{String.class,Object.class,Object.class});
	@Override
	public void warn(String msg, Object... arg0) {
		if (threshold > WARN)
			return;

		realLogger.warn(msg, arg0);
	}

	// warnST = slf4j.getMethod("warn", new Class[]{String.class,Throwable.class});
	@Override
	public void warn(String msg, Throwable th) {
		if (threshold > WARN)
			return;

		// we treat RuntimeException and Error as an error
		if (th instanceof RuntimeException || th instanceof Error)
			realLogger.warn("ERROR:  " + msg, th); //$NON-NLS-1$
		else if (threshold != ERROR)
			realLogger.warn(msg, th);
	}

	// errorST = slf4j.getMethod("error", new Class[]{String.class,Throwable.class});
	public void error(String msg, Throwable th) {
		if (threshold > ERROR)
			return;

		realLogger.warn("ERROR:  " + msg, th); //$NON-NLS-1$
	}

	@Override
	public String getName() {
		return realLogger.getName();
	}

	@Override
	public void warn(Throwable thrown) {
		if (threshold > WARN)
			return;
		realLogger.warn(thrown);
	}

	@Override
	public void info(Throwable thrown) {
		if (threshold > INFO)
			return;
		realLogger.info(thrown);
	}

	@Override
	public void info(String msg, Throwable thrown) {
		if (threshold > INFO)
			return;
		realLogger.info(msg, thrown);
	}

	@Override
	public boolean isDebugEnabled() {
		return threshold == DEBUG;
	}

	@Override
	public void setDebugEnabled(boolean enabled) {
		threshold = DEBUG;
	}

	@Override
	public void debug(Throwable thrown) {
		if (threshold > DEBUG)
			return;
		realLogger.debug(thrown);
	}

	@Override
	public void ignore(Throwable ignored) {
		// Just post this to debug
		debug(ignored);
	}

	@Override
	public void debug(String msg, long value) {
		if (threshold > DEBUG)
			return;
		realLogger.debug(msg, value);
	}
}

Back to the top