1717
1818package com .teragrep .jla_04 ;
1919
20+ import java .io .File ;
21+ import java .io .FileInputStream ;
22+ import java .io .FileNotFoundException ;
23+ import java .io .IOException ;
2024import java .net .InetAddress ;
2125import java .net .UnknownHostException ;
26+ import java .util .logging .Formatter ;
27+ import java .util .logging .LogManager ;
28+ import java .util .logging .LogRecord ;
29+ import java .util .logging .SimpleFormatter ;
2230
2331public class RelpConfig {
2432 private int port ; // Relp server port
@@ -32,11 +40,15 @@ public class RelpConfig {
3240 private int reconnectInterval ; // sleep between relp connection reconnects
3341 private int readTimeout ; // relp connection reading timeout
3442 private int writeTimeout ; // relp connection writing timeout
43+ private LogManager manager ;
44+ private Formatter formatter ;
3545
36- public RelpConfig (String name ) throws NumberFormatException , IllegalArgumentException , NoSuchFieldException {
46+ public RelpConfig (String name ) throws NumberFormatException , IllegalArgumentException , NoSuchFieldException , ClassNotFoundException , InstantiationException , IllegalAccessException {
47+ initLogger ();
3748 // Name must be set first, others do not depend on the ordering
3849 initName (name );
3950 // The rest
51+ initFormatter ();
4052 initPort ();
4153 initAddress ();
4254 initAppname ();
@@ -49,15 +61,71 @@ public RelpConfig(String name) throws NumberFormatException, IllegalArgumentExce
4961 initWriteTimeout ();
5062 }
5163
64+ private void initLogger () {
65+ this .manager = LogManager .getLogManager ();
66+ String configpath = System .getProperty ("java.util.logging.config.file" );
67+ if (configpath != null ) {
68+ File configfile = new File (configpath );
69+ if (!configfile .exists () || !configfile .isFile ()) {
70+ System .out .println ("Can't find properties file at " + configpath );
71+ return ;
72+ }
73+ FileInputStream inputStream = null ;
74+ try {
75+ inputStream = new FileInputStream (configpath );
76+ LogManager .getLogManager ().readConfiguration (inputStream );
77+ } catch (Exception e ) {
78+ e .printStackTrace ();
79+ }
80+ }
81+ }
82+
83+ private void initFormatter () throws ClassNotFoundException , InstantiationException , IllegalAccessException {
84+ // Get normal prop and fallback to manager prop; from that fallback to simple formatter
85+ String formatter_name = System .getProperty ("java.util.logging.RelpHandler." + this .getName () + ".formatter" , this .manager .getProperty ("java.util.logging.RelpHandler." + this .getName () + ".formatter" ));
86+ if (formatter_name != null ) {
87+ ClassLoader classloader = ClassLoader .getSystemClassLoader ();
88+ if (classloader == null ) {
89+ System .out .println ("Unable to initialize ClassLoader.getSystemClassLoader(), defaulting to SimpleFormatter" );
90+ }
91+ if (classloader != null ) {
92+ Object formatter_object = classloader .loadClass (formatter_name ).newInstance ();
93+ this .formatter = (Formatter ) formatter_object ;
94+ }
95+ }
96+ else {
97+ this .formatter = new SimpleFormatter () {
98+ @ Override
99+ public synchronized String format (LogRecord logrecord ) {
100+ return String .format ("%1$s" , logrecord .getMessage ());
101+ }
102+ };
103+ }
104+ }
105+
106+ public Formatter getFormatter () {
107+ return this .formatter ;
108+ }
109+
52110 private String getProperty (String name , String fallback ) {
111+ // Overrides from props
53112 String prop = System .getProperty ("java.util.logging.RelpHandler." + this .getName () + "." + name );
54- if (prop == null ) {
55- return fallback ;
113+ if (prop != null ) {
114+ if (prop .equals ("" )) {
115+ throw new IllegalArgumentException ("Field is set but has no value: java.util.logging.RelpHandler." + this .getName () + "." + name );
116+ }
117+ return prop ;
56118 }
57- if (prop .equals ("" )) {
58- throw new IllegalArgumentException ("Field is set but has no value: java.util.logging.RelpHandler." + this .getName () + "." + name );
119+ // Check if values are set in prop file
120+ String from_prop = this .manager .getProperty ("java.util.logging.RelpHandler." + this .getName () + "." + name );
121+ if (from_prop != null ) {
122+ if (from_prop .equals ("" )) {
123+ throw new IllegalArgumentException ("Field is set but has no value: java.util.logging.RelpHandler." + this .getName () + "." + name );
124+ }
125+ return from_prop ;
59126 }
60- return prop ;
127+ // Default
128+ return fallback ;
61129 }
62130
63131 private void initName (String name ) throws NoSuchFieldException , IllegalArgumentException {
0 commit comments