Skip to content

Commit cb95a93

Browse files
author
kadary DEMBELE
committed
Add of new features:
- The framework is fully configurable in java and from structured property file - User can switch from logger to logger without reconfiguration
1 parent 2b1abf0 commit cb95a93

9 files changed

Lines changed: 215 additions & 65 deletions

File tree

javaLogger.properties

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LevelINFO=truE
88
LevelDEBUG=true
99
LevelWARN=TRUE
1010
LevelERROR=true
11-
LevelTrace=false
11+
LevelTrace=true
1212
#Default output Handlers
1313
#File Handler:
1414
FileHandler=true
@@ -33,4 +33,28 @@ Separator=|
3333
#You can add an unlimited logger specific configuration here #
3434
#Note that, if you omit a parameter for a logger, the default #
3535
#value for that parameter will be use. #
36-
###############################################################
36+
#Loggers configured here overwrite java configuration #
37+
###############################################################
38+
#The first line in this section list all available loggers
39+
#loggers=logger1, logger2, ....
40+
41+
#You can configured you declared loggers on the first line by this model
42+
#logger1.Name=com.foo
43+
#logger1.Level=TRACE
44+
#logger1.Handlers=com.handlers.handler1,com.handlers.handler2, ...
45+
#logger1.LogFilePath=my/log/file/path
46+
#logger1.LogFileSize=2048
47+
#logger1.Formater=com.formater.Myformater
48+
49+
#Example
50+
loggers=esiea, demkada
51+
#Example of a fully configured logger
52+
esiea.Name=fr.esiea.JavaLogger
53+
esiea.Level=DEBUG
54+
esiea.Handlers=project.architecture.javaLogger.modules.output.handler.ConsoleHandler, project.architecture.javaLogger.modules.output.handler.FileHandler
55+
esiea.LogFilePath=D:/logs/l1.log
56+
esiea.LogFileSize=5
57+
esiea.Formater= project.architecture.javaLogger.test.TestFormater
58+
59+
#Example of only declared logger, this logger will use default config or java Config
60+
demkada.Name=com.demkada.test

src/project/architecture/javaLogger/core/AbstractLogger.java

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Set;
88

99
import project.architecture.javaLogger.modules.config.Key;
10+
import project.architecture.javaLogger.modules.output.formater.DefaultFormater;
1011
import project.architecture.javaLogger.modules.output.formater.Formater;
1112
import project.architecture.javaLogger.modules.output.handler.ConsoleHandler;
1213
import project.architecture.javaLogger.modules.output.handler.DataBaseHandler;
@@ -28,10 +29,56 @@ public abstract class AbstractLogger implements Logger {
2829
protected Map<String, Handler> handlers = new HashMap<String, Handler>();
2930
protected Level levelFixed;
3031
protected Formater formater;
31-
private Properties settings = LogManager.config.getSettings();
32+
protected Properties settings = LogManager.config.getSettings();
3233

3334
public AbstractLogger(String name) {
34-
this.setFQCN(name);
35+
String configClass = name + ".Name";
36+
String configLevel = name + ".Level";
37+
String configHandlers = name + ".Handlers";
38+
String configFormater = name + ".Formater";
39+
40+
if(settings.containsKey(configClass)) {
41+
this.setFQCN((String) settings.get(configClass));
42+
//System.out.println(this.fqcn);
43+
}
44+
else
45+
this.setFQCN(name);
46+
47+
if(settings.containsKey(configLevel)) {
48+
this.setLevel(Level.getLevel((String) settings.get(configLevel)));
49+
//System.out.println(this.levelFixed.name);
50+
}
51+
52+
if(settings.containsKey(configHandlers)) {
53+
String[] handlers = settings.getProperty(configHandlers).split(",");
54+
for(int i = 0; i < handlers.length; i++) {
55+
Class<?> configHandler;
56+
try {
57+
configHandler = Class.forName(handlers[i].trim());
58+
this.addHandlerClass(configHandler);
59+
//System.out.println(configHandler.getCanonicalName());
60+
} catch (ClassNotFoundException e) {
61+
System.out.print("The class you have specified as handler doe's not exist: ");
62+
e.printStackTrace();
63+
}
64+
}
65+
}
66+
67+
if(settings.containsKey(configFormater)) {
68+
String formater = settings.getProperty(configFormater);
69+
Class<?> formaterClass;
70+
try {
71+
formaterClass = Class.forName(formater.trim());
72+
this.setFormaterClass(formaterClass);
73+
} catch (ClassNotFoundException e) {
74+
setFormater(new DefaultFormater());
75+
System.out.print("The class you have specified as framater doe's not exist: ");
76+
e.printStackTrace();
77+
}
78+
}
79+
else
80+
setFormater(new DefaultFormater());
81+
3582
}
3683

3784
public abstract void trace(String message);
@@ -90,6 +137,19 @@ public boolean isTraceEnabled() {
90137
public void addHandler(Handler handler) {
91138
handlers.put(handler.getClass().getName(), handler);
92139
}
140+
private void addHandlerClass(Class<?> configHandler) {
141+
try {
142+
Handler handler = (Handler) configHandler.newInstance();
143+
this.addHandler(handler);
144+
//System.out.println(this.handlers.toString());
145+
} catch (InstantiationException e) {
146+
System.out.print("Unable to instantiate your provided handler: ");
147+
e.printStackTrace();
148+
} catch (IllegalAccessException e) {
149+
System.out.print("Can't access to your provided handler: ");
150+
e.printStackTrace();
151+
}
152+
}
93153

94154
@Override
95155
public void setLevel(Level levelFixed) {
@@ -100,6 +160,19 @@ public void setLevel(Level levelFixed) {
100160
public void setFormater(Formater formater) {
101161
this.formater = formater;
102162
}
163+
private void setFormaterClass(Class<?> formaterClass) {
164+
try {
165+
Formater formater = (Formater) formaterClass.newInstance();
166+
this.setFormater(formater);
167+
//System.out.println(this.formater.getClass().getCanonicalName());
168+
} catch (InstantiationException e) {
169+
System.out.print("Unable to instantiate your provided formater: ");
170+
e.printStackTrace();
171+
} catch (IllegalAccessException e) {
172+
System.out.print("Can't access to your provided formater: ");
173+
e.printStackTrace();
174+
}
175+
}
103176

104177
protected void logByLoggerConfig(String message, Level level) {
105178
if (!handlers.isEmpty()) {

src/project/architecture/javaLogger/core/DefaultLogger.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package project.architecture.javaLogger.core;
22

3-
import project.architecture.javaLogger.modules.output.formater.DefaultFormater;
43

54

65
/**
@@ -12,7 +11,6 @@ public class DefaultLogger extends AbstractLogger {
1211

1312
public DefaultLogger(String name) {
1413
super(name);
15-
setFormater(new DefaultFormater());
1614
}
1715

1816
@Override
Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package project.architecture.javaLogger.core;
22

3+
34
/**
45
* Herited class with our 6 log levels : TRACE > ERROR > DEBUG > WARM > INFO > OFF
56
* off - Logger disabled
@@ -13,22 +14,44 @@
1314
* @version 1.1
1415
*/
1516
public class Level extends AbstractLevel {
16-
17+
1718
public Level(String name, int value) {
1819
this.setName(name);
1920
this.setValue(value);
2021
}
21-
22+
23+
public static Level getLevel(String levelName) {
24+
Level level;
25+
switch (levelName) {
26+
case "INFO":
27+
level = Level.INFO;
28+
break;
29+
case "WARN":
30+
level = Level.WARN;
31+
break;
32+
case "DEBUG":
33+
level = Level.DEBUG;
34+
break;
35+
case "ERROR":
36+
level = Level.ERROR;
37+
break;
38+
default:
39+
level = Level.TRACE;
40+
break;
41+
}
42+
return level;
43+
}
44+
2245
public static final Level OFF = new Level("OFF", Integer.MIN_VALUE);
23-
46+
2447
public static final Level INFO = new Level("INFO", 100);
25-
48+
2649
public static final Level WARN = new Level("WARN", 200);
27-
50+
2851
public static final Level DEBUG = new Level("DEBUG", 300);
29-
52+
3053
public static final Level ERROR = new Level("ERROR", 400);
31-
54+
3255
public static final Level TRACE = new Level("TRACE", Integer.MAX_VALUE);
33-
56+
3457
}

src/project/architecture/javaLogger/core/LogManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6-
import project.architecture.javaLogger.modules.config.ConfigFromProperties;
76
import project.architecture.javaLogger.modules.config.Configurator;
7+
import project.architecture.javaLogger.modules.config.DefaultConfigurator;
88

99
/**
1010
* Principal class of Logger. it's must be imported in project by customer
@@ -15,8 +15,8 @@ public class LogManager {
1515

1616
public static final Map<String, Logger> loggers = new HashMap<String, Logger>();
1717
private String fqcn;
18-
public static final Configurator config = new ConfigFromProperties();
19-
18+
public static final Configurator config = new DefaultConfigurator();
19+
2020
/**
2121
* Return string log
2222
* @param <T>
@@ -37,7 +37,7 @@ public <T> Logger getLogger(Class<T> targetClass) {
3737

3838
public Logger getLogger(String targetClass) {
3939
if (targetClass != null & targetClass != "") {
40-
this.fqcn = targetClass;
40+
this.fqcn = targetClass;
4141
}
4242
if (loggers.containsKey(fqcn)) {
4343
return loggers.get(fqcn);

src/project/architecture/javaLogger/modules/config/ConfigFromProperties.java renamed to src/project/architecture/javaLogger/modules/config/DefaultConfigurator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @author mhgeay, kadary
1212
* @version 1.0
1313
*/
14-
public class ConfigFromProperties extends AbstractConfigurator {
14+
public class DefaultConfigurator extends AbstractConfigurator {
1515

1616
public final static Properties config = new Properties();
1717

@@ -22,7 +22,7 @@ public class ConfigFromProperties extends AbstractConfigurator {
2222
* @return Properties config
2323
*/
2424

25-
public ConfigFromProperties() {
25+
public DefaultConfigurator() {
2626

2727
setConfigFilePath("JavaLogger.properties");
2828

@@ -32,7 +32,7 @@ public ConfigFromProperties() {
3232
configFileStream = new FileInputStream(getConfigFilePath());
3333
try {
3434
config.load(configFileStream);
35-
// System.out.println(config.toString());
35+
//System.out.println(config.toString());
3636
configFileStream.close();
3737
}
3838
catch (IOException e) {

0 commit comments

Comments
 (0)