Skip to content

Commit fd528b0

Browse files
author
kadary DEMBELE
committed
Add of Java Configuration Functionalities:
- All logger configuration overwrite Property File configuration - Add of tests cases - Code refactoring
1 parent 2b78f3c commit fd528b0

12 files changed

Lines changed: 344 additions & 266 deletions

File tree

Lines changed: 107 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11
package project.architecture.javaLogger.modules.core;
22

3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
import java.util.Map;
36
import java.util.Properties;
7+
import java.util.Set;
48

59
import project.architecture.javaLogger.modules.config.Key;
610
import project.architecture.javaLogger.modules.output.ConsoleHandler;
711
import project.architecture.javaLogger.modules.output.DataBaseHandler;
812
import project.architecture.javaLogger.modules.output.FileHandler;
913
import project.architecture.javaLogger.modules.output.Handler;
10-
import project.architecture.javaLogger.modules.output.Target;
1114

1215

1316
/**
1417
* @author kadary
1518
* @version 1.0
19+
* @param <V>
1620
*/
1721
public class AbstractLogger implements Logger {
18-
22+
1923
private String fqcn;
20-
private Handler console = new ConsoleHandler();
21-
private Handler file = new FileHandler();
22-
private Handler db = new DataBaseHandler();
24+
private Handler CONSOLE = new ConsoleHandler();
25+
private Handler FILE = new FileHandler();
26+
private Handler DB = new DataBaseHandler();
27+
protected Map<String, Handler> handlers = new HashMap<String, Handler>();
28+
protected Level levelFixed ;
2329
private Properties settings = LogManager.config.getSettings();
24-
25-
public AbstractLogger(String name) {
26-
this.setFQCN(name);
27-
}
2830

29-
public String getFQCN() {
31+
public AbstractLogger(String name) {
32+
this.setFQCN(name);
33+
}
34+
35+
private String getFQCN() {
3036
return fqcn;
3137
}
3238

33-
public void setFQCN(String fqcn) {
39+
private void setFQCN(String fqcn) {
3440
this.fqcn = fqcn;
3541
}
3642

37-
public boolean isEnabled(String level) {
43+
public boolean isEnabled(String levelFixed) {
3844
String value;
3945
boolean result = false;
4046
try {
41-
if (settings.get(level) != null) {
42-
value = (String) settings.get(level);
47+
if (settings.get(levelFixed) != null) {
48+
value = (String) settings.get(levelFixed);
4349
result = value.equalsIgnoreCase("true") ? true : false;
4450
}
4551
}
@@ -55,16 +61,12 @@ public boolean isInfoEnabled() {
5561
}
5662

5763
public void info(String message) {
58-
if (isEnabled(Key.ConsoleHandler.name())) {
59-
console.log(Level.INFO, message, this.getFQCN(), Target.CONSOLE);
64+
if (!handlers.isEmpty() || !isNull(levelFixed)) {
65+
this.logByLoggerConfig(message, Level.INFO);
6066
}
61-
62-
if (isEnabled(Key.FileHandler.name())) {
63-
file.log(Level.INFO, message, this.getFQCN(), Target.FILE);
64-
}
65-
66-
if (isEnabled(Key.DataBaseHandler.name())) {
67-
db.log(Level.INFO, message, this.getFQCN(), Target.DB);
67+
68+
else {
69+
this.logByPropConfig(message, Level.INFO);
6870
}
6971
}
7072

@@ -73,16 +75,11 @@ public boolean isWarnEnabled() {
7375
}
7476

7577
public void warn(String message) {
76-
if (isEnabled(Key.ConsoleHandler.name())) {
77-
console.log(Level.WARN, message, this.getFQCN(), Target.CONSOLE);
78+
if (!handlers.isEmpty() || !isNull(levelFixed)) {
79+
this.logByLoggerConfig(message, Level.WARN);
7880
}
79-
80-
if (isEnabled(Key.FileHandler.name())) {
81-
file.log(Level.WARN, message, this.getFQCN(), Target.FILE);
82-
}
83-
84-
if (isEnabled(Key.DataBaseHandler.name())) {
85-
db.log(Level.WARN, message, this.getFQCN(), Target.DB);
81+
else {
82+
this.logByPropConfig(message, Level.WARN);
8683
}
8784
}
8885

@@ -91,16 +88,11 @@ public boolean isErrorEnabled() {
9188
}
9289

9390
public void error(String message) {
94-
if (isEnabled(Key.ConsoleHandler.name())) {
95-
console.log(Level.ERROR, message, this.getFQCN(), Target.CONSOLE);
96-
}
97-
98-
if (isEnabled(Key.FileHandler.name())) {
99-
file.log(Level.ERROR, message, this.getFQCN(), Target.FILE);
91+
if (!handlers.isEmpty() || !isNull(levelFixed)) {
92+
this.logByLoggerConfig(message, Level.ERROR);
10093
}
101-
102-
if (isEnabled(Key.DataBaseHandler.name())) {
103-
db.log(Level.ERROR, message, this.getFQCN(), Target.DB);
94+
else {
95+
this.logByPropConfig(message, Level.ERROR);
10496
}
10597
}
10698

@@ -109,16 +101,12 @@ public boolean isDebugEnabled() {
109101
}
110102

111103
public void debug(String message) {
112-
if (isEnabled(Key.ConsoleHandler.name())) {
113-
console.log(Level.DEBUG, message, this.getFQCN(), Target.CONSOLE);
114-
}
115-
116-
if (isEnabled(Key.FileHandler.name())) {
117-
file.log(Level.DEBUG, message, this.getFQCN(), Target.FILE);
104+
if (!handlers.isEmpty() || !isNull(levelFixed)) {
105+
this.logByLoggerConfig(message, Level.DEBUG);
118106
}
119-
120-
if (isEnabled(Key.DataBaseHandler.name())) {
121-
db.log(Level.DEBUG, message, this.getFQCN(), Target.DB);
107+
108+
else {
109+
this.logByPropConfig(message, Level.DEBUG);
122110
}
123111
}
124112

@@ -129,17 +117,77 @@ public boolean isTraceEnabled() {
129117

130118
@Override
131119
public void trace(String message) {
132-
if (isEnabled(Key.ConsoleHandler.name())) {
133-
console.log(Level.TRACE, message, this.getFQCN(), Target.CONSOLE);
120+
if (!handlers.isEmpty() || !isNull(levelFixed)) {
121+
this.logByLoggerConfig(message, Level.TRACE);
122+
}
123+
124+
else {
125+
this.logByPropConfig(message, Level.TRACE);
126+
}
127+
}
128+
129+
@Override
130+
public void setHandlers(Handler handler) {
131+
handlers.put(handler.getClass().getName(), handler);
132+
}
133+
134+
@Override
135+
public void setLevel(Level levelFixed) {
136+
this.levelFixed = levelFixed;
137+
}
138+
139+
@Override
140+
public void setLayout() {
141+
// TODO Auto-generated method stub
142+
143+
}
144+
145+
private void logByLoggerConfig(String message, Level level) {
146+
if (!handlers.isEmpty()) {
147+
Set<String> keys = handlers.keySet();
148+
Iterator<String> iterator = keys.iterator();
149+
while(iterator.hasNext()) {
150+
String key = iterator.next();
151+
Handler handler = handlers.get(key);
152+
if(!isNull(levelFixed)) {
153+
handler.log(level, message, this.getFQCN(), handler.getClass().getName(), levelFixed);
154+
}
155+
else
156+
handler.log(level, message, this.getFQCN(), handler.getClass().getName());
157+
}
134158
}
135-
136-
if (isEnabled(Key.FileHandler.name())) {
137-
file.log(Level.TRACE, message, this.getFQCN(), Target.FILE);
159+
else if(!isNull(levelFixed)) {
160+
if (isEnabled(Key.ConsoleHandler.name())) {
161+
CONSOLE.log(level, message, this.getFQCN(), ConsoleHandler.class.getName(), levelFixed);
162+
}
163+
164+
if (isEnabled(Key.FileHandler.name())) {
165+
FILE.log(level, message, this.getFQCN(), FileHandler.class.getName(), levelFixed);
166+
}
167+
168+
if (isEnabled(Key.DataBaseHandler.name())) {
169+
DB.log(level, message, this.getFQCN(), DataBaseHandler.class.getName(), levelFixed);
170+
}
138171
}
139-
140-
if (isEnabled(Key.DataBaseHandler.name())) {
141-
db.log(Level.TRACE, message, this.getFQCN(), Target.DB);
172+
}
173+
174+
private void logByPropConfig(String message, Level level) {
175+
if (handlers.isEmpty() & isNull(levelFixed)) {
176+
if (isEnabled(Key.ConsoleHandler.name())) {
177+
CONSOLE.log(level, message, this.getFQCN(), ConsoleHandler.class.getName());
178+
}
179+
180+
if (isEnabled(Key.FileHandler.name())) {
181+
FILE.log(level, message, this.getFQCN(), FileHandler.class.getName());
182+
}
183+
184+
if (isEnabled(Key.DataBaseHandler.name())) {
185+
DB.log(level, message, this.getFQCN(), DataBaseHandler.class.getName());
186+
}
142187
}
143-
188+
}
189+
190+
private boolean isNull(Level levelFixed) {
191+
return this.levelFixed == null || this.levelFixed.getName() == " " ? true : false;
144192
}
145193
}

src/project/architecture/javaLogger/modules/core/Level.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,4 @@ public Level(String name, int value) {
3030
public static final Level ERROR = new Level("ERROR", 100);
3131

3232
public static final Level OFF = new Level("OFF", Integer.MIN_VALUE);
33-
34-
35-
3633
}

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,31 @@
1313
*/
1414
public class LogManager {
1515

16-
private Map<String, Logger> loggers = new HashMap<String, Logger>();
16+
public static final Map<String, Logger> loggers = new HashMap<String, Logger>();
1717
private String fqcn;
1818
public static final Configurator config = new ConfigFromProperties();
1919

20-
public LogManager() {
21-
fqcn = Thread. currentThread().getStackTrace()[2].getClassName();
22-
}
23-
2420
/**
2521
* Return string log
22+
* @param <T>
2623
* @param name [Fully-Qualified Class Name]
2724
* @return Full log : Date+Time FQCN Level Log message
2825
*/
29-
public Logger getLogger(String name) {
30-
if (name != null && name != "") {
31-
this.fqcn = name;
26+
public <T> Logger getLogger(Class<T> targetClass) {
27+
if (targetClass != null) {
28+
this.fqcn = targetClass.getName();
29+
}
30+
if (loggers.containsKey(fqcn)) {
31+
return loggers.get(fqcn);
32+
}
33+
else
34+
loggers.put(fqcn, new ExtendedLogger(fqcn));
35+
return loggers.get(fqcn);
36+
}
37+
38+
public Logger getLogger(String targetClass) {
39+
if (targetClass != null & targetClass != "") {
40+
this.fqcn = targetClass;
3241
}
3342
if (loggers.containsKey(fqcn)) {
3443
return loggers.get(fqcn);
@@ -39,7 +48,7 @@ public Logger getLogger(String name) {
3948
}
4049

4150
public Logger getLogger() {
42-
return this.getLogger(fqcn);
51+
return this.getLogger(Thread. currentThread().getStackTrace()[2].getClassName());
4352
}
4453

4554
public void closeLogger(String name) {

src/project/architecture/javaLogger/modules/core/Logger.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package project.architecture.javaLogger.modules.core;
22

3+
import project.architecture.javaLogger.modules.output.Handler;
4+
35

46

57
/**
@@ -28,8 +30,10 @@ public interface Logger {
2830
// ERROR
2931
boolean isErrorEnabled();
3032
void error(String message);
31-
32-
// Custom path (FQCN)
33-
String getFQCN();
34-
void setFQCN(String fqcn);
33+
34+
void setHandlers(Handler handler);
35+
36+
void setLevel(Level level);
37+
38+
void setLayout();
3539
}
Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,14 @@
11
package project.architecture.javaLogger.modules.output;
22

3-
import java.util.Properties;
4-
5-
import project.architecture.javaLogger.modules.config.Key;
63
import project.architecture.javaLogger.modules.core.Level;
7-
import project.architecture.javaLogger.modules.core.LogManager;
84

95
/**
106
* @author kadary
117
* @version 1.0
128
*/
139
public abstract class AbstractHandler implements Handler {
1410

15-
private Properties settings = LogManager.config.getSettings();
16-
17-
public abstract void log(Level level, String message, String fqcn, Target target);
18-
19-
20-
public boolean isEnabled(String token) {
21-
String value;
22-
boolean result = false;
23-
try {
24-
if (settings.get(token) != null) {
25-
value = (String) settings.get(token);
26-
result = value.equalsIgnoreCase("true") ? true : false;
27-
}
28-
}
29-
catch (NullPointerException e) {
30-
System.out.print("Settings not set! please check your config: ");
31-
e.printStackTrace();
32-
}
33-
return result;
34-
}
35-
36-
public boolean isLevelEnabled() {
37-
return isEnabled(Key.ShowLEVEL.name());
38-
}
39-
40-
public boolean isDateEnabled() {
41-
return isEnabled(Key.ShowDATE.name());
42-
}
43-
44-
public boolean isFQCNEnabled() {
45-
return isEnabled(Key.ShowFQCN.name());
46-
}
11+
public abstract void log(Level level, String message, String fqcn, String handler);
12+
public abstract void log(Level level, String message, String fqcn, String handler, Level levelFixed);
4713

4814
}

0 commit comments

Comments
 (0)