1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package io.github.prolobjectlink.prolog.jlog;
23
24 import java.io.IOException;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.logging.FileHandler;
28 import java.util.logging.Formatter;
29 import java.util.logging.Handler;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32
33 import io.github.prolobjectlink.prolog.AbstractLogger;
34 import io.github.prolobjectlink.prolog.PrologLogger;
35
36
37
38
39
40
41 final class JLogLogger extends AbstractLogger implements PrologLogger {
42
43 private static final String MESSAGE = "Logger File Handler can't be created";
44 private static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
45
46 public JLogLogger() {
47 this(Level.INFO);
48 }
49
50 private JLogLogger(Level level) {
51 LOGGER.setLevel(level);
52 Logger rootlogger = LOGGER.getParent();
53 SimpleDateFormat f = new SimpleDateFormat("yyyy.MM.dd");
54 String date = f.format(new Date());
55 Formatter formatter = new JLogFormatter();
56 for (Handler h : rootlogger.getHandlers()) {
57 h.setFormatter(formatter);
58 h.setLevel(level);
59 }
60 FileHandler file = null;
61 try {
62 file = new FileHandler("%t/prolobjectlink-" + date + ".log", true);
63 } catch (SecurityException e) {
64 rootlogger.log(Level.SEVERE, MESSAGE, e);
65 } catch (IOException e) {
66 rootlogger.log(Level.SEVERE, MESSAGE, e);
67 }
68 assert file != null;
69 file.setFormatter(formatter);
70 LOGGER.addHandler(file);
71 }
72
73 public void log(Object sender, Level level, Object message, Throwable throwable) {
74 LOGGER.log(level, sender + "\n" + message, throwable);
75 }
76
77 }