]> jfr.im git - irc/rizon/acid.git/blob - acid/src/main/java/net/rizon/acid/logging/Logger.java
4ff932c6a0efc8047c009deca38ec5a2ccb72d66
[irc/rizon/acid.git] / acid / src / main / java / net / rizon / acid / logging / Logger.java
1 package net.rizon.acid.logging;
2
3 import ch.qos.logback.classic.Level;
4 import ch.qos.logback.classic.spi.ILoggingEvent;
5 import ch.qos.logback.classic.spi.IThrowableProxy;
6 import ch.qos.logback.classic.spi.StackTraceElementProxy;
7 import ch.qos.logback.core.UnsynchronizedAppenderBase;
8 import java.util.ArrayList;
9 import java.util.List;
10 import net.rizon.acid.core.AcidCore;
11 import net.rizon.acid.core.Acidictive;
12 import net.rizon.acid.core.User;
13 import net.rizon.acid.util.Format;
14
15 public class Logger extends UnsynchronizedAppenderBase<ILoggingEvent>
16 {
17
18 private String calculateColor(Level level)
19 {
20 switch (level.toInt())
21 {
22 case Level.DEBUG_INT:
23 return Format.color(Format.GREY);
24 case Level.INFO_INT:
25 return "";
26 case Level.WARN_INT:
27 return Format.color(Format.ORANGE);
28 case Level.ERROR_INT:
29 return Format.color(Format.RED);
30 default:
31 return "";
32 }
33 }
34
35 @Override
36 protected void append(ILoggingEvent event)
37 {
38 Level level = event.getLevel();
39 if (!level.isGreaterOrEqual(Level.INFO))
40 {
41 return;
42 }
43
44 String message = event.getFormattedMessage();
45 IThrowableProxy throwable = event.getThrowableProxy();
46 StackTraceElement[] stes = null;
47
48 if (throwable != null)
49 {
50 List<StackTraceElement> list = new ArrayList<>();
51 for (StackTraceElementProxy step : throwable.getStackTraceElementProxyArray())
52 {
53 list.add(step.getStackTraceElement());
54 }
55 stes = list.toArray(new StackTraceElement[0]);
56 }
57
58 if (AcidCore.me != null && !AcidCore.me.isBursting() && User.findUser(Acidictive.conf.general.control) != null)
59 {
60 String routingSpam = Acidictive.conf.getChannelNamed("routing-spam");
61
62 if (message != null)
63 {
64 Acidictive.privmsg(routingSpam, calculateColor(event.getLevel()) + Format.BOLD + event.getLevel() + Format.BOLD + Format.color(0) + " (" + event.getCallerData()[0].getFileName() + "): " + message);
65 }
66
67 if (throwable != null)
68 {
69 String exception = throwable.getMessage() == null ? throwable.getClassName() : throwable.getMessage();
70
71 if (exception != null)
72 {
73 Acidictive.privmsg(routingSpam, exception);
74 }
75 }
76 if (stes != null)
77 {
78 for (StackTraceElement ste : stes)
79 {
80 Acidictive.privmsg(routingSpam, ste.toString());
81 }
82 }
83 }
84 }
85 }