-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathLogger.java
More file actions
85 lines (73 loc) · 2.16 KB
/
Logger.java
File metadata and controls
85 lines (73 loc) · 2.16 KB
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
package com.example.task01;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Logger {
private String Name;
private Level currentLevel = Level.DEBUG;
private static final Map<String, Logger> loggers = new HashMap<>();
public enum Level {
DEBUG(0),
INFO(1),
WARNING(2),
ERROR(3);
private final int priority;
Level(int priority) {
this.priority = priority;
}
public int getPriority() { return priority; }
}
Logger(String name){
this.Name = name;
}
public void setName(String name){
this.Name = name;
}
public String getName(){
return this.Name;
}
public Level getLevel() {
return currentLevel;
}
public void setLevel(Level level) {
this.currentLevel = level;
}
public static Logger getLogger(String name) {
return loggers.computeIfAbsent(name, Logger::new);
}
public void log(Level lvl, String message)
{
if (lvl.getPriority() >= currentLevel.getPriority()) {
String timestamp = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(new Date());
System.out.printf("[%s] %s %s - %s%n", lvl.name(), timestamp, Name, message);
}
}
public void log(Level lvl, String message, Object... args) {
log(lvl, String.format(message, args));
}
public void debug(String message) {
log(Level.DEBUG, message);
}
public void debug(String message, Object... args) {
log(Level.DEBUG, message, args);
}
public void info(String message) {
log(Level.INFO, message);
}
public void info(String message, Object... args) {
log(Level.INFO, message, args);
}
public void warning(String message) {
log(Level.WARNING, message);
}
public void warning(String message, Object... args) {
log(Level.WARNING, message, args);
}
public void error(String message) {
log(Level.ERROR, message);
}
public void error(String message, Object... args) {
log(Level.ERROR, message, args);
}
}