-
Notifications
You must be signed in to change notification settings - Fork 92
/
FluentLogger.java
158 lines (128 loc) · 4.4 KB
/
FluentLogger.java
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
//
// A Structured Logger for Fluent
//
// Copyright (C) 2011 - 2013 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.fluentd.logger;
import java.util.HashMap;
import java.util.Map;
import org.fluentd.logger.errorhandler.ErrorHandler;
import org.fluentd.logger.sender.Reconnector;
import org.fluentd.logger.sender.Sender;
public class FluentLogger {
private static FluentLoggerFactory factory = new FluentLoggerFactory();
public static FluentLogger getLogger(String tagPrefix) {
return factory.getLogger(tagPrefix, "localhost", 24224);
}
public static FluentLogger getLogger(String tagPrefix, String host, int port) {
return factory.getLogger(tagPrefix, host, port, 3 * 1000, 1 * 1024 * 1024);
}
public static synchronized FluentLogger getLogger(String tagPrefix, String host, int port, int timeout, int bufferCapacity) {
return factory.getLogger(tagPrefix, host, port, timeout, bufferCapacity);
}
public static synchronized FluentLogger getLogger(String tagPrefix, String host, int port, int timeout,
int bufferCapacity, Reconnector reconnector) {
return factory.getLogger(tagPrefix, host, port, timeout, bufferCapacity, reconnector);
}
/**
* the method is for testing
*/
static Map<FluentLogger, String> getLoggers() {
return factory.getLoggers();
}
public static synchronized void closeAll() {
factory.closeAll();
}
public static synchronized void flushAll() {
factory.flushAll();
}
protected String tagPrefix;
protected Sender sender;
protected FluentLogger() {
}
protected FluentLogger(String tagPrefix, Sender sender) {
this.tagPrefix = tagPrefix;
this.sender = sender;
}
public boolean log(String tag, String key, Object value) {
return log(tag, key, value, 0);
}
public boolean log(String tag, String key, Object value, long timestamp) {
Map<String, Object> data = new HashMap<String, Object>();
data.put(key, value);
return log(tag, data, timestamp);
}
public boolean log(String tag, Map<String, Object> data) {
return log(tag, data, 0);
}
public boolean log(String tag, Map<String, Object> data, long timestamp) {
String concatTag = null;
if (tagPrefix == null || tagPrefix.length() == 0) {
concatTag = tag;
}
else {
concatTag = tagPrefix + "." + tag;
}
if (timestamp != 0) {
return sender.emit(concatTag, timestamp, data);
} else {
return sender.emit(concatTag, data);
}
}
public void flush() {
sender.flush();
}
public void close() {
if (sender != null) {
sender.flush();
sender.close();
sender = null;
}
factory.purgeLogger(this);
}
public String getName() {
return String.format("%s_%s", tagPrefix, sender.getName());
}
@Override
public String toString() {
return String.format("%s{tagPrefix=%s,sender=%s}",
new Object[] { this.getClass().getName(), tagPrefix, sender.toString() });
}
@Override
public void finalize() {
if (sender != null) {
sender.close();
}
}
public boolean isConnected() {
return sender != null && sender.isConnected();
}
public synchronized void setErrorHandler(ErrorHandler errorHandler) {
if (errorHandler == null) {
throw new IllegalArgumentException("errorHandler is null");
}
if (sender != null) {
sender.setErrorHandler(errorHandler);
}
}
public synchronized void removeErrorHandler() {
if (sender != null) {
sender.removeErrorHandler();
}
}
public Sender getSender() {
return sender;
}
}