forked from reposense/RepoSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogsManager.java
More file actions
145 lines (123 loc) · 4.68 KB
/
LogsManager.java
File metadata and controls
145 lines (123 loc) · 4.68 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
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
package reposense.system;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
/**
* Configures and manages the loggers and handlers, including their levels
*/
public class LogsManager {
// Whenever the log file size exceeds {@code MAX_FILE_SIZE_IN_BYTES} it rolls over to another file
// The maximum number of files to store the logs is {@code FILE_COUNT}
private static final int FILE_COUNT = 2;
private static final int MEGABYTE = (1 << 20);
private static final int MAX_FILE_SIZE_IN_BYTES = 5 * MEGABYTE; // 5MB
private static final ArrayList<Logger> LOGGER_LIST = new ArrayList<>();
// All the log files will be store with a .log extension
// eg. reposense.log.0, in the logs/ folder of the working directory
private static final String LOG_FOLDER_NAME = "logs";
private static final String LOG_FILE_NAME = "reposense.log";
private static Path logFolderLocation;
private static Level currentConsoleLogLevel = Level.INFO;
private static Level currentFileLogLevel = Level.INFO;
private static FileHandler fileHandler;
private static ConsoleHandler consoleHandler;
public static Logger getLogger(String name) {
Logger logger = Logger.getLogger(name);
LOGGER_LIST.add(logger);
logger.setUseParentHandlers(false);
removeHandlers(logger);
addConsoleHandler(logger);
if (logFolderLocation != null) {
addFileHandler(logger);
}
return logger;
}
/**
* Creates a {@code Logger} for the given class name.
*/
public static <T> Logger getLogger(Class<T> clazz) {
if (clazz == null) {
return Logger.getLogger("");
}
return getLogger(clazz.getSimpleName());
}
/**
* Adds the Console Handler to the {@code logger}.
* Creates the Console Handler if it is null.
*/
private static void addConsoleHandler(Logger logger) {
if (consoleHandler == null) {
consoleHandler = createConsoleHandler();
}
logger.addHandler(consoleHandler);
}
/**
* Removes all the handlers from {@code logger}.
*/
private static void removeHandlers(Logger logger) {
Handler[] handlers = logger.getHandlers();
for (Handler handler : handlers) {
logger.removeHandler(handler);
}
}
/**
* Adds the File Handler to the {@code logger}.
* Creates File Handler if it is null.
*/
private static void addFileHandler(Logger logger) {
Path path = logFolderLocation.resolve(LOG_FOLDER_NAME);
try {
if (!Files.exists(path)) {
Files.createDirectories(path);
logger.info("Log folder has been successfully created");
}
if (fileHandler == null) {
fileHandler = createFileHandler();
}
logger.addHandler(fileHandler);
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
/**
* Creates a {@code FileHandler} for the log file.
*
* @throws IOException if there are problems opening the file.
*/
private static FileHandler createFileHandler() throws IOException {
Path path = logFolderLocation.resolve(LOG_FOLDER_NAME).resolve(LOG_FILE_NAME);
FileHandler fileHandler = new FileHandler(path.toString(), MAX_FILE_SIZE_IN_BYTES, FILE_COUNT, true);
fileHandler.setFormatter(new SimpleFormatter());
fileHandler.setLevel(currentFileLogLevel);
return fileHandler;
}
/**
* Creates a {@code ConsoleHandler} to output the log to console.
*/
private static ConsoleHandler createConsoleHandler() {
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(currentConsoleLogLevel);
consoleHandler.setFormatter(new CustomLogFormatter());
return consoleHandler;
}
/** Sets the log folder location using {@code location} and adds file handler with this location to all the loggers
* created.
*/
public static void setLogFolderLocation(Path location) {
logFolderLocation = location;
LOGGER_LIST.stream().forEach(logger -> addFileHandler(logger));
}
public static void setConsoleHandlerLevel(Level level) {
currentConsoleLogLevel = level;
}
public static void setFileConsoleHandlerLevel(Level level) {
currentFileLogLevel = level;
}
}