-
Notifications
You must be signed in to change notification settings - Fork 33
/
Logger.java
1467 lines (1338 loc) · 54.4 KB
/
Logger.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2020 Payara Services Ltd.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.util.log;
import java.util.Properties;
import java.util.MissingResourceException;
import java.util.Vector;
import java.util.Enumeration;
import java.util.TimeZone;
import java.util.logging.ErrorManager;
import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.text.MessageFormat;
import com.sun.messaging.jmq.util.LoggerWrapper;
import com.sun.messaging.jmq.util.StringUtil;
import com.sun.messaging.jmq.resources.SharedResources;
import com.sun.messaging.jmq.util.MQResourceBundle;
import org.glassfish.hk2.api.ServiceLocator;
/**
* Logger is an interface to a set of LogHandlers (logging devices). LogHandlers are added using setLogHandlers(). Once
* the log handlers are set you log messages to them using the Logger's various log() and logStack() methods. You may
* specify a resource bundle for Logger to use via the setResourceBundle() method. If a resource bundle is set Logger
* will attempt to lookup up messages in it using the string passed to the log() method as a key. If the lookup fails
* then the string is assumed to be the text to log, and not a key.
*
* Logger defines six log levels in the following descending order: ERROR, WARNING, INFO, DEBUG, DEBUGMED, DEBUGHIGH. By
* default Logger will only display INFO messages and above. Logger's 'level' field is public so that code can
* efficiently compare it if they want to avoid the overhead of a method call. Note that in addition to Logger's log
* level, each LogHandler may selectively choose which messages to log by specifying a bit-mask for the log levels they
* want messages for.
*
* When a Logger is in a closed() state, any messages logged to it are deferred in a buffer. When the logger is opened
* the deferred messages are logged.
*/
public class Logger implements LoggerWrapper {
// Resource bundle to look up logged messages
private MQResourceBundle rb = null;
// standard java util logger
private static java.util.logging.Logger newLogger = null;
public static final Level EMERGENCY = ForceLogLevel.FORCE;
public static final String GFLOGFILEKEY = "com.sun.enterprise.server.logging.GFFileHandler.file";
public static final String JULLOGFILEKEY = "java.util.logging.FileHandler.pattern";
// Key for System log handler identity prop
public static final String SYSLOGHANDLRIDENTKEY = "imq.log.syslog.identity";
// new logger name
public static final String LOGGERNAME = "imq.log.Logger";
// Buffer to hold messages that are logged when Logger is closed
private Vector deferBuffer = null;
private boolean closed = false;
// Full path to "home" directory for Logger. Any relative paths
// used by logger will be relative to this directory.
String logHome = null;
String propPrefix = "";
// Current logging level
public int level = INFO;
/**
* Log levels. We use bit values so that LogHandlers can selectively choose which level of messages to log.
*/
public static final int FORCE = 0x0040; // Should always be greatest
public static final int ERROR = 0x0020;
public static final int WARNING = 0x0010;
public static final int INFO = 0x0008;
public static final int DEBUG = 0x0004;
public static final int DEBUGMED = 0x0002;
public static final int DEBUGHIGH = 0x0001;
public static final int OFF = Integer.MAX_VALUE;
/**
* A hack. These are the properties that may be dynamically updated. Logger really shouldn't know about handler specific
* properties.
*/
private static final String LOGLEVEL_PROP = ".level";
private static final String TIMEZONE_PROP = "log.timezone";
private static final String ROLLOVERSECS_PROP = "log.file.rolloversecs";
private static final String ROLLOVERBYTES_PROP = "java.util.logging.FileHandler.limit";
// Resource bundle for the Logging code to use to display it's error
// messages.
private static SharedResources myrb = SharedResources.getResources();
// Format of date string prepended to each message
private SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss z");
/**
* Constructor for Logger.
*
* @param logHome Home directory for this Logger. Any relative paths used by this Logger will be relative to logHome.
*/
public Logger(String logHome) {
this.logHome = logHome;
// Create standard JUL logger for later handoff
newLogger = java.util.logging.Logger.getLogger(LOGGERNAME);
}
public void useMilliseconds() {
df = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss:SS z");
}
public int getLevel() {
return level;
}
/**
* Get the name of the properties that can be dynamically updated. These are prefixed with whatever prefix string was
* passed to configure()
*/
public String[] getUpdateableProperties() {
String[] v = new String[4];
v[0] = propPrefix + "." + LOGLEVEL_PROP;
v[1] = propPrefix + "." + ROLLOVERSECS_PROP;
v[2] = propPrefix + "." + ROLLOVERBYTES_PROP;
v[3] = propPrefix + "." + TIMEZONE_PROP;
return v;
}
/**
* Dynamically update a property.
*
* @throws IllegalArgumentException
*/
public synchronized void updateProperty(String name, String value) {
if (name == null || value == null || value.equals("")) {
return;
}
// Skip over <prefix>.
name = name.substring(propPrefix.length() + 1);
long n = -1;
if (name.equals(LOGLEVEL_PROP)) {
this.level = levelStrToInt(value);
Level julLevel = levelIntToJULLevel(level);
newLogger.setLevel(julLevel);
} else if (name.equals(TIMEZONE_PROP)) {
if (value.length() > 0) {
df.setTimeZone(TimeZone.getTimeZone(value));
}
} else if (name.equals(ROLLOVERSECS_PROP) || name.equals(ROLLOVERBYTES_PROP)) {
// Hack! Logger should not know about these properties
try {
n = Long.parseLong(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(myrb.getString(myrb.W_BAD_NFORMAT, name, value));
}
java.util.logging.Handler[] handlers = null;
if (newLogger.getUseParentHandlers()) {
handlers = newLogger.getParent().getHandlers();
} else {
handlers = newLogger.getHandlers();
}
FileHandler flh = null;
for (java.util.logging.Handler handler : handlers) {
if (handler instanceof FileHandler) {
flh = (FileHandler) handler;
break;
}
}
if (flh == null) {
return;
}
/*
* As of MQ 3.5, the value that is used to mean 'unlimited' is -1 (although 0 is still supported). To support -1 as an
* additional way of specifying unlimited, this method will detect if -1 was set for rolloverbytes or rolloversecs
* (these are the properties that are relevant here), and pass on the value '0' to the method setRolloverLimits().
*
*/
if (n == -1) {
n = 0L;
}
} else {
throw new IllegalArgumentException(myrb.getString(myrb.X_BAD_PROPERTY, name));
}
}
/**
* Find the handler with the specified name private synchronized LogHandler findHandler(String name) {
*
* if (handlers == null) { return null; }
*
* // Return the handler that matches the passed name for (int n = 0; n < handlers.length; n++) { if (handlers[n] !=
* null && name.equals(handlers[n].getName())) { return handlers[n]; } } return null; }
*/
/**
* @param inProcess true if caller is running in someone else's JVM
* @param jmsraManaged true if caller's lifecycle is managed by JMSRA
*
* Configure the logger based on the configuration in the passed properties. The properties and their syntax is TBD
*/
public synchronized void configure(Properties props, String prefix, boolean inProcess, boolean jmsraManaged, ServiceLocator habitat) {
propPrefix = prefix;
if (props == null) {
return;
}
// close();
String property = null, value = null;
Level julLevel = null;
property = prefix + ".log." + "level";
value = props.getProperty(property);
if (value != null && !value.trim().equals("")) {
try {
this.level = levelStrToInt(value);
julLevel = levelIntToJULLevel(this.level);
} catch (IllegalArgumentException e) {
this.level = INFO;
julLevel = null;
this.log(WARNING, myrb.getKString(myrb.W_BAD_LOGLEVELSTR, property, value));
}
}
property = ".level";
value = props.getProperty(property);
if (value != null && !value.trim().equals("")) {
try {
julLevel = Level.parse(value);
this.level = levelJULLevelToInt(julLevel);
} catch (IllegalArgumentException e) {
this.level = INFO;
julLevel = null;
this.log(WARNING, myrb.getKString(myrb.W_BAD_LOGLEVELSTR, property, value));
}
}
if (julLevel != null) {
newLogger.setLevel(julLevel);
}
// load properties to nucleus when we are not running inside nucleus
// This is only until we completely moved to nucleus env
loadPropsToNucleusLogging(props, habitat, inProcess, jmsraManaged);
// Correct ErrorManager system out with stacktrace to just message.
java.util.logging.Handler[] handlers = null;
if (newLogger.getUseParentHandlers()) {
handlers = newLogger.getParent().getHandlers();
} else {
handlers = newLogger.getHandlers();
}
for (final java.util.logging.Handler handler : handlers) {
handler.setErrorManager(new ErrorManager() {
private boolean reported = false;
/**
* The error method is called when a Handler failure occurs. We will override this to print just an error message
* instead of full stacktrace printing. Main reason: JIRA 145
* <p>
* This method may be overriden in subclasses. The default behavior in this base class is that the first call is
* reported to System.err, and subsequent calls are ignored.
*
* @param msg a descriptive string (may be null)
* @param ex an exception (may be null)
* @param code an error code defined in ErrorManager
*/
@Override
public synchronized void error(String msg, Exception ex, int code) {
if (reported) {
// We only report the first error, to avoid clogging
// the screen.
return;
}
reported = true;
String text = "java.util.logging.Logger Trouble logging:";
if (msg != null) {
text = text + ": " + msg;
}
System.err.println(text + ex.getClass().getName());
// if (ex != null && handler.getLevel().equals(Level.FINEST)) {
// ex.printStackTrace();
// }
}
});
}
// if(newLogger != null) return;
//
// String property, value;
//
// prefix = prefix + ".log.";
//
// // Get the log level
// property = prefix + "level";
// value = props.getProperty(property);
// if (value != null && !value.equals("")) {
// try {
// this.level = levelStrToInt(value);
// } catch (IllegalArgumentException e) {
// this.log(WARNING,
// myrb.getKString(myrb.W_BAD_LOGLEVELSTR, property, value));
// }
// }
//
// property = prefix + "timezone";
// value = props.getProperty(property);
// if (value != null && !value.equals("")) {
// df.setTimeZone(TimeZone.getTimeZone(value));
// }
//
// // Get list of LogHandlers from the properties
// property = prefix + "handlers";
// value = props.getProperty(property);
// if (value == null || value.equals("")) {
// this.log(ERROR, myrb.getKString(myrb.E_NO_LOGHANDLERLIST,
// property));
// return;
// }
//
// String handlerName;
// String handlerClass;
//
// StringTokenizer token = new StringTokenizer(value, ",", false);
// int nHandlers = token.countTokens();
// LogHandler[] handlers = new LogHandler[nHandlers];
//
// // Loop through each LogHandler and instantiate the class
// for (int n = 0; n < nHandlers; n++) {
// handlerName = token.nextToken();
// property = prefix + handlerName + ".class";
// handlerClass = props.getProperty(property);
// if (handlerClass == null || handlerClass.equals("")) {
// this.log(ERROR, myrb.getKString(myrb.E_NO_LOGHANDLER,
// property));
// continue;
// }
//
// Class t;
// try {
// if (habitat != null) {
// handlers[n] = (LogHandler)habitat.getByType(handlerClass);
// if (handlers[n] == null) {
// t = Class.forName(handlerClass);
// handlers[n] = (LogHandler)t.newInstance();
// }
// } else {
// t = Class.forName(handlerClass);
// handlers[n] = (LogHandler)t.newInstance();
// }
// } catch (ClassNotFoundException e) {
// this.log(ERROR, myrb.getKString(myrb.E_BAD_LOGHANDLERCLASS,
// e.toString()));
// continue;
// } catch (IllegalAccessException e) {
// this.log(ERROR, myrb.getKString(myrb.E_BAD_LOGHANDLERCLASS,
// e.toString()));
// continue;
// } catch (InstantiationException e) {
// this.log(ERROR, myrb.getKString(myrb.E_BAD_LOGHANDLERCLASS,
// e.toString()));
// continue;
// }
// handlers[n].init(this);
// handlers[n].setName(handlerName);
//
// try {
// // Configure LogHandler class based on properties
// handlers[n].configure(props, prefix + handlerName);
// } catch (IllegalArgumentException e) {
// this.log(WARNING, myrb.getKString(myrb.W_BAD_LOGCONFIG,
// e.toString()));
// } catch (UnsatisfiedLinkError e) {
// this.log(WARNING, myrb.getKString(myrb.W_LOGCHANNEL_DISABLED,
// handlerName, e.getMessage()));
// handlers[n] = null;
//
//
// } catch (java.lang.NoClassDefFoundError err) {
// this.log(WARNING, myrb.getKString(myrb.W_LOGCHANNEL_DISABLED,
// handlerName, err.getMessage()));
// handlers[n] = null;
// }
// }
//
// try {
// setLogHandlers(handlers);
// } catch (IOException e) {
// // Not I18N because this is a programming error
// System.err.println("Could not set handlers: " + e);
// }
}
/**
* Load properties file to nucleus logging framework. This will allow us to use properties file that is JMS specific
* instead of nucleus logging.properties
***/
private void loadPropsToNucleusLogging(Properties props, ServiceLocator habitat, boolean inProcess, boolean jmsraManaged) {
// If we are running inside nucleus we will be using properties from nucleus logging.properties
if (habitat != null) {
return;
}
// Following is a workaround to avoid system out message for "Bad level value for property: sun.os.patch.level"
final String propToBeRemoved = "sun.os.patch.level";
if (props.containsKey(propToBeRemoved)) {
props.remove(propToBeRemoved);
}
// This means let's feed logging framework paths and other logging properties for standalone env
// If we don't have glasshfish install root property set formatter does system out "installRoot null" msg
// that breaks some of ourtests as well. This is a workaround only for outside nucleus env case.
Properties p = System.getProperties();
String installRoot = StringUtil.expandVariables("${imq.instanceshome}${/}${imq.instancename}", props);
if (!(inProcess && jmsraManaged) && System.getProperty("com.sun.aas.installRoot") == null) {
p.put("com.sun.aas.installRoot", installRoot);
}
String key = JULLOGFILEKEY;
String origValue = null;
String expandedValue = null;
if ((origValue = props.getProperty(key)) != null) {
// Expand possible variables like ${jmq.varhome} and update value
expandedValue = StringUtil.expandVariables(origValue, props);
props.put(key, expandedValue);
this.publish(DEBUG, "File logger log file location property key=" + key + ", value=" + expandedValue);
// Create log dir as logging framework will fail creating file otherwise
File d = new File(expandedValue);
if (!d.getParentFile().exists()) {
boolean rcode = d.getParentFile().mkdir();
if (rcode == false) {
publish(ERROR, myrb.getString(myrb.X_DIR_CREATE, d.getParentFile().getAbsolutePath()));
}
}
}
key = GFLOGFILEKEY;
origValue = null;
expandedValue = null;
if ((origValue = props.getProperty(key)) != null) {
// Expand possible variables like ${jmq.varhome} and update value
expandedValue = StringUtil.expandVariables(origValue, props);
props.put(key, expandedValue);
this.publish(DEBUG, "File logger log file location property key=" + key + ", value=" + expandedValue);
// Create log dir as logging framework will fail creating file otherwise
File d = new File(expandedValue);
if (!d.getParentFile().exists()) {
boolean rcode = d.getParentFile().mkdir();
if (rcode == false) {
publish(ERROR, myrb.getString(myrb.X_DIR_CREATE, d.getParentFile().getAbsolutePath()));
}
}
}
key = SYSLOGHANDLRIDENTKEY;
origValue = null;
expandedValue = null;
if ((origValue = props.getProperty(key)) != null) {
// Expand possible variables like ${jmq.varhome} and update value
expandedValue = StringUtil.expandVariables(origValue, props);
props.put(key, expandedValue);
this.publish(DEBUG, "System loghandler identity property key=" + key + ", value=" + expandedValue);
}
ByteArrayOutputStream propsStream = new ByteArrayOutputStream();
// If Embeded RA Broker configure properties as added handler for jms logger and do not over load logger properties
// To avoid disturbing glassfish/application server logging properties
if (inProcess && jmsraManaged) {
manuallyConfigureLogging(props);
return;
}
try {
props.store(propsStream, "Loading into stream to pass on to nucleus logging framework");
LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(propsStream.toByteArray()));
} catch (IOException e) {
this.log(ERROR, "Pushing jms properties file to logging framework failed", e);
}
}
/**
* Instead of using LogManager to configure logging framework do it manually This is mainly due to embeded RA broker
* case where we do not have luxary to overwrite application server logging properties that will be overwritten if
* broker properties are loaded to LogManager
*
* @param props broker configuration properties
*/
private void manuallyConfigureLogging(Properties props) {
newLogger.setUseParentHandlers(false);
String[] handlerClasses = parseClassNames(props.getProperty("handlers"));
for (String handlerClass : handlerClasses) {
Handler newHandler = null;
try {
Class clz = getClass().getClassLoader().loadClass(handlerClass);
if (clz.equals(FileHandler.class)) {
String pattern = props.containsKey(handlerClass + ".pattern") ? props.getProperty(handlerClass + ".pattern") : null;
int limit = props.containsKey(handlerClass + ".limit") ? Integer.parseInt(props.getProperty(handlerClass + ".limit")) : 0;
if (limit < 0) {
limit = 0;
}
int count = props.containsKey(handlerClass + ".count") ? Integer.parseInt(props.getProperty(handlerClass + ".count")) : 1;
if (count <= 0) {
count = 1;
}
boolean append = props.containsKey(handlerClass + ".append") && Boolean.parseBoolean(props.getProperty(handlerClass + ".append"));
newHandler = new FileHandler(pattern, limit, count, append);
// Set default formatter to glassfish formatter and not xml
newHandler.setFormatter(new com.sun.messaging.jmq.util.log.UniformLogFormatter());
} else if (clz.equals(SysLogHandler.class)) {
newHandler = new SysLogHandler(props);
} else {
newHandler = (java.util.logging.Handler) clz.getDeclaredConstructor().newInstance();
}
try {
// Check if there is a property defining the
// this handler's level.
String levs = props.getProperty(handlerClass + ".level");
if (levs != null) {
newHandler.setLevel(Level.parse(levs));
}
configureCommonHandler(newHandler, props);
} catch (Exception ex) {
System.err.println("Can't set level for " + handlerClass);
// Probably a bad level. Drop through.
}
newLogger.addHandler(newHandler);
} catch (Exception ex) {
System.err.println("Can't load config class \"" + handlerClass + "\"");
System.err.println("" + ex);
// ex.printStackTrace();
}
}
}
// Helper method to configure Common LogHandler outside LogManager
private void configureCommonHandler(Handler handler, Properties props) {
String baseProp = handler.getClass().getName();
String filterStr = props.getProperty(baseProp + ".filter");
Filter filter = null;
try {
if (filterStr != null) {
Class clz = ClassLoader.getSystemClassLoader().loadClass(filterStr);
filter = (Filter) clz.getDeclaredConstructor().newInstance();
handler.setFilter(filter);
}
} catch (Exception ex) {
// We got one of a variety of exceptions in creating the
// class or creating an instance.
// Drop through.
}
String formatterStr = props.getProperty(baseProp + ".formatter");
Formatter formatter = getFormatter(formatterStr);
if (formatter != null) {
handler.setFormatter(formatter);
}
}
// Get formatter object from string configuration
private Formatter getFormatter(String formatterString) {
Formatter formatter = null;
try {
if (formatterString != null) {
Class clz = ClassLoader.getSystemClassLoader().loadClass(formatterString);
formatter = (Formatter) clz.getDeclaredConstructor().newInstance();
}
} catch (Exception ex) {
// We got one of a variety of exceptions in creating the
// class or creating an instance.
// Drop through.
}
return formatter;
}
// get a list of whitespace separated classnames from a property.
private String[] parseClassNames(String classNamePropertyValue) {
if (classNamePropertyValue == null) {
return new String[0];
}
classNamePropertyValue = classNamePropertyValue.trim();
int ix = 0;
Vector<String> result = new Vector<>();
while (ix < classNamePropertyValue.length()) {
int end = ix;
while (end < classNamePropertyValue.length()) {
if (Character.isWhitespace(classNamePropertyValue.charAt(end))) {
break;
}
if (classNamePropertyValue.charAt(end) == ',') {
break;
}
end++;
}
String word = classNamePropertyValue.substring(ix, end);
ix = end + 1;
word = word.trim();
if (word.length() == 0) {
continue;
}
result.add(word);
}
return result.toArray(new String[result.size()]);
}
/**
* Convert a string representation of a log level to its integer value.
*
* @param levelStr Log level string. One of: ERROR, WARNING, INFO, DEBUG DEBUGMED, DEBUGHIGH
*
* @throws IllegalArgumentException
*/
public static int levelStrToInt(String levelStr) {
if (levelStr.equals("FORCE")) {
return Logger.FORCE;
} else if (levelStr.equals("ERROR")) {
return Logger.ERROR;
} else if (levelStr.equals("WARNING")) {
return Logger.WARNING;
} else if (levelStr.equals("INFO")) {
return Logger.INFO;
} else if (levelStr.equals("DEBUG")) {
return Logger.DEBUG;
} else if (levelStr.equals("DEBUGMED")) {
return Logger.DEBUGMED;
} else if (levelStr.equals("DEBUGHIGH")) {
return Logger.DEBUGHIGH;
} else if (levelStr.equals("NONE")) {
return Logger.OFF;
} else {
throw new IllegalArgumentException(myrb.getString(myrb.W_BAD_LOGLEVELSTR, levelStr));
}
}
/**
* Convert an int representation of a log level to its string value.
*
* @param level Log level int. One of: ERROR, WARNING, INFO, DEBUG DEBUGMED, DEBUGHIGH
*
* @throws IllegalArgumentException
*/
public static String levelIntToStr(int level) {
switch (level) {
case FORCE:
return "FORCE";
case ERROR:
return "ERROR";
case WARNING:
return "WARNING";
case INFO:
return "INFO";
case DEBUG:
return "DEBUG";
case DEBUGMED:
return "DEBUGMED";
case DEBUGHIGH:
return "DEBUGHIGH";
default:
final String errmsg = "Conversion from custom log level int " + level + " to String failed";
throw new IllegalArgumentException(errmsg);
}
}
/**
* Convert an JUL int representation of a log level to its string value.
*
* @param level Log level int. One of:ALL,FORCE,SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST, OFF
*
* @throws IllegalArgumentException
*/
public static String jullevelIntToStr(int level) {
if (level == Level.ALL.intValue()) {
return Level.ALL.getName();
} else if (level == EMERGENCY.intValue()) {
return EMERGENCY.getName();
} else if (level == Level.SEVERE.intValue()) {
return Level.SEVERE.getName();
} else if (level == Level.WARNING.intValue()) {
return Level.WARNING.getName();
} else if (level == Level.INFO.intValue()) {
return Level.INFO.getName();
} else if (level == Level.CONFIG.intValue()) {
return Level.CONFIG.getName();
} else if (level == Level.FINE.intValue()) {
return Level.FINE.getName();
} else if (level == Level.FINER.intValue()) {
return Level.FINER.getName();
} else if (level == Level.FINEST.intValue()) {
return Level.FINEST.getName();
} else if (level == Level.OFF.intValue()) {
return Level.OFF.getName();
} else {
final String errmsg = "Conversion from JUL log level int " + level + " to String failed";
throw new IllegalArgumentException(errmsg);
}
}
/**
* Convert an int representation of a log level to its equivalent java.util.logging.LogLevel.
*
* @param level Log level int. One of: ERROR, WARNING, INFO, DEBUG DEBUGMED, DEBUGHIGH
*
* @throws IllegalArgumentException
*/
public static Level levelIntToJULLevel(int level) {
switch (level) {
case FORCE:
return EMERGENCY; // TODO: Need to wait for nucleus team to add custom log level
case ERROR:
return Level.SEVERE;
case WARNING:
return Level.WARNING;
case INFO:
return Level.INFO;
case DEBUG:
return Level.FINE;
case DEBUGMED:
return Level.FINER;
case DEBUGHIGH:
return Level.FINEST;
case OFF:
return Level.OFF;
default:
final String errmsg = "Conversion from " + level + " to JULLevel failed";
newLogger.log(java.util.logging.Level.SEVERE, errmsg);
throw new IllegalArgumentException(errmsg);
}
}
/**
* Convert an java.util.logging.LogLevel representation of a log level to its equivalent int value.
*
* @param level Log level int. One of: ERROR, WARNING, INFO, DEBUG DEBUGMED, DEBUGHIGH
*
* @throws IllegalArgumentException
*/
public static int levelJULLevelToInt(Level level) {
if (level.equals(EMERGENCY)) {
return FORCE; // TODO: Need to wait for nucleus team to add custom log level
} else if (level.equals(Level.SEVERE)) {
return ERROR;
} else if (level.equals(Level.WARNING)) {
return WARNING;
} else if (level.equals(Level.INFO)) {
return INFO;
} else if (level.equals(Level.FINE)) {
return DEBUG;
} else if (level.equals(Level.FINER)) {
return DEBUGMED;
} else if (level.equals(Level.FINEST)) {
return DEBUGHIGH;
} else if (level.equals(Level.OFF)) {
return OFF;
} else {
final String errmsg = "Conversion from " + level + " to int failed";
newLogger.log(Level.SEVERE, errmsg);
throw new IllegalArgumentException(errmsg);
}
}
/**
* Set the resource bundle used to lookup strings logged via the log() and logStack() methods
*
* @param rb The resource bundle used to lookup logged strings.
*/
public void setResourceBundle(MQResourceBundle rb) {
this.rb = rb;
}
/**
* Close all LogHandlers
*/
public synchronized void close() {
// Close handlers
java.util.logging.Handler[] handlers = null;
if (newLogger.getUseParentHandlers()) {
handlers = newLogger.getParent().getHandlers();
} else {
handlers = newLogger.getHandlers();
}
if (handlers != null) {
for (final java.util.logging.Handler handler : handlers) {
handler.close();
}
}
closed = true;
}
// We can not reopen JUL log handler once it is closed based on current knowledge
// /**
// * Open all LogHandlers. If a handler fails to open and message
// * is written to System.err and the handler is removed from the
// * list of LogHandlers.
// *
// * If there are any messages in the defer buffer they are flushed
// * out after the handlers are opened.
// */
// public synchronized void open() {
//
// // Open handlers
// for (int n = 0; n < handlers.length; n++) {
// if (handlers[n] != null) {
// try {
// handlers[n].open();
// } catch (IOException e) {
// this.log(ERROR, myrb.getKString(myrb.E_BAD_LOGDEVICE,
// handlers[n].toString(),
// e));
// handlers[n] = null;
// }
// }
// }
//
// closed = false;
//
// // Flush any defered messages to handlers
// flushDeferBuffer();
// }
/**
* Flush all LogHandlers.
*/
public void flush() {
// Flush handlers
// if (handlers != null) {
// for (int n = 0; n < handlers.length; n++) {
// if (handlers[n] != null) {
// handlers[n].flush();
// handlers[n].getStdHandler().flush();
// }
// }
// }
}
/**
* Publish a formatted message to all LogHandlers. If the Logger is closed, then the message is held in a buffer until
* the Logger is opened.
*
* @param level Log level. Message will be logged to a LogHandler if the handler accepts this level of message. If level
* is FORCE then message will be published to all LogHandlers unless a handlers has its log level set to NONE.
*
* @param message The string to log. It is assumed that this string is already formatted
*
*/
public void publish(int level, String message) {
// If the Logger is closed we defer the message into a buffer
// until the Logger is opened again
if (closed) {
if (newLogger.getHandlers() == null || newLogger.getHandlers().length == 0) {
defer(level, message);
return;
} else {
flushDeferBuffer();
closed = false;
}
}
boolean loggedOnce = false;
// Delegate logging calls to new standard JUL logger
try {
newLogger.log(levelIntToJULLevel(level), message);
} catch (Exception e) {
// We got in trouble logging message so let's put it in defer area until it is resolved
defer(level, message);
closed = true;
}
loggedOnce = true;
// The message has already been formatted. We just need to
// send it to each LogHandler
// for (int n = 0; n < handlers.length; n++) {
// try {
// handler = handlers[n];
// } catch (Exception e) {
// // If list was changed out from under us (unlikely)
// // just skip handler
// handler = null;
// }
// // Check if handler wants this level of message
// if (handler != null &&
// handler.levels != 0 &&
// ((level == FORCE && handler.isAllowForceMessage()) || (level != FORCE && (level & handler.levels) != 0))) {
// try {
// handler.publish(level, message);
// loggedOnce = true;
// } catch (IOException e) {
// System.err.println(myrb.getKString(myrb.E_LOGMESSAGE,
// handler.toString(), e));
// }
// }
// }
// Don't let message go into black hole.
if (!loggedOnce) {
System.err.println(message);
}
}
/**
* Defers a message into a buffer until the buffer is flushed to the handlers. When a Logger is closed, any messages
* logged are defered until the buffer is opened
*/
private synchronized void defer(int level, String message) {
if (deferBuffer == null) {
deferBuffer = new Vector(32);
}
// Add message to buffer. We must preserve the level too.
LogRecord dr = new LogRecord(level, message);
deferBuffer.addElement(dr);
}
/**
* Flushes deferred messages out of buffer and releases the buffer
*/
private synchronized void flushDeferBuffer() {
if (deferBuffer == null) {
return;
}
// Publish all messages in buffer
LogRecord df = null;
for (Enumeration e = deferBuffer.elements(); e.hasMoreElements();) {
df = (LogRecord) e.nextElement();
publish(df.level, df.message);
}
// Clear buffer and discard it
deferBuffer.clear();
deferBuffer = null;
}
/**
* Format a log message.
*
* @param level Log level. Used to generate a possible prefix like "ERROR" or "WARNING"
*
* @param key Key to use to find message in resource bundle. If message is not localized then the key is just the raw
* message to display.
*
* @param args Optional args to message. Null if no args
*
* @param ex Optional throwable to display description of. Null if no throwable.
*
* @param printStack True to print stack trace of throwable. False to not print a stack trace. If True "ex" must not be
* null.
*
*/
public String format(int level, String key, Object[] args, Throwable ex, boolean printStack) {
StringBuilder sb = new StringBuilder(80);
// // Append date and time
// sb.append("[");
// sb.append(df.format(new Date()));