Skip to content

Commit 9b93e45

Browse files
committed
Added logback plugin files
1 parent 83f5a7f commit 9b93e45

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

Diff for: plugins/logback/pom.xml

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.kairosdb</groupId>
9+
<artifactId>metrics4j-all</artifactId>
10+
<version>${revision}</version>
11+
<relativePath>../../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>m4j-logback</artifactId>
15+
<name>metrics4j-logback</name>
16+
<description>Metrics4j plugin to read logback metrics</description>
17+
<url>https://github.com/kairosdb/metrics4j</url>
18+
19+
<scm>
20+
<url>https://github.com/kairosdb/metrics4j</url>
21+
<connection>scm:git:https://github.com/kairosdb/metrics4j.git</connection>
22+
</scm>
23+
24+
<licenses>
25+
<license>
26+
<name>The Apache Software License, Version 2.0</name>
27+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
28+
<distribution>repo</distribution>
29+
</license>
30+
</licenses>
31+
<developers>
32+
<developer>
33+
<id>brianhks</id>
34+
<name>Brian</name>
35+
<email>[email protected]</email>
36+
</developer>
37+
</developers>
38+
39+
<properties>
40+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
41+
</properties>
42+
43+
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.kairosdb</groupId>
47+
<artifactId>metrics4j</artifactId>
48+
<version>${revision}</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.slf4j</groupId>
53+
<artifactId>slf4j-api</artifactId>
54+
<version>${slf4j-version}</version>
55+
<scope>provided</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>ch.qos.logback</groupId>
59+
<artifactId>logback-classic</artifactId>
60+
<version>1.5.15</version>
61+
</dependency>
62+
</dependencies>
63+
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-assembly-plugin</artifactId>
69+
<version>3.2.0</version>
70+
71+
<configuration>
72+
<descriptors>
73+
<descriptor>src/assembly.xml</descriptor>
74+
</descriptors>
75+
</configuration>
76+
77+
<executions>
78+
<execution>
79+
<id>jar-with-deps</id>
80+
<phase>package</phase>
81+
<goals>
82+
<goal>single</goal>
83+
</goals>
84+
</execution>
85+
</executions>
86+
87+
</plugin>
88+
</plugins>
89+
</build>
90+
91+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.kairosdb.metrics.metrics4jplugin.logback;
2+
3+
import org.kairosdb.metrics4j.annotation.Help;
4+
import org.kairosdb.metrics4j.annotation.Key;
5+
import org.kairosdb.metrics4j.collectors.LongCollector;
6+
7+
public interface LoggerStats
8+
{
9+
@Help("Number of calls made to the logger, with the level as a tag")
10+
LongCollector logCount(@Key("name") String name, @Key("level") String level, @Key("logger")String logger);
11+
12+
@Help("Number of calls made to the trace logger")
13+
LongCollector trace(@Key("name") String name, @Key("logger")String logger);
14+
@Help("Number of calls made to the debug logger")
15+
LongCollector debug(@Key("name") String name, @Key("logger")String logger);
16+
@Help("Number of calls made to the info logger")
17+
LongCollector info(@Key("name") String name, @Key("logger")String logger);
18+
@Help("Number of calls made to the warn logger")
19+
LongCollector warn(@Key("name") String name, @Key("logger")String logger);
20+
@Help("Number of calls made to the error logger")
21+
LongCollector error(@Key("name") String name, @Key("logger")String logger);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.kairosdb.metrics.metrics4jplugin.logback;
2+
3+
import ch.qos.logback.classic.Level;
4+
import ch.qos.logback.classic.spi.ILoggingEvent;
5+
import ch.qos.logback.core.AppenderBase;
6+
import org.kairosdb.metrics4j.MetricSourceManager;
7+
8+
import static ch.qos.logback.classic.Level.DEBUG;
9+
import static ch.qos.logback.classic.Level.ERROR;
10+
import static ch.qos.logback.classic.Level.INFO;
11+
import static ch.qos.logback.classic.Level.TRACE;
12+
import static ch.qos.logback.classic.Level.WARN;
13+
14+
15+
/**
16+
Simple appender for use with logback applications. Add this appender to your
17+
application logback.xml file, and it will send counts of log entries to your
18+
favorite metrics system.
19+
*/
20+
public class Metrics4jAppender extends AppenderBase<ILoggingEvent>
21+
{
22+
private static final LoggerStats stats = MetricSourceManager.getSource(LoggerStats.class);
23+
24+
25+
@Override
26+
protected void append(ILoggingEvent event)
27+
{
28+
String logger = event.getLoggerName();
29+
switch (event.getLevel().toInt()) {
30+
case Level.TRACE_INT:
31+
stats.logCount(name, TRACE.levelStr, logger).put(1);
32+
stats.trace(name, logger).put(1);
33+
break;
34+
case Level.DEBUG_INT:
35+
stats.logCount(name, DEBUG.levelStr, logger).put(1);
36+
stats.debug(name, logger).put(1);
37+
break;
38+
case Level.INFO_INT:
39+
stats.logCount(name, INFO.levelStr, logger).put(1);
40+
stats.info(name, logger).put(1);
41+
break;
42+
case Level.WARN_INT:
43+
stats.logCount(name, WARN.levelStr, logger).put(1);
44+
stats.warn(name, logger).put(1);
45+
break;
46+
case Level.ERROR_INT:
47+
stats.logCount(name, ERROR.levelStr, logger).put(1);
48+
stats.error(name, logger).put(1);
49+
break;
50+
default:
51+
break;
52+
}
53+
}
54+
55+
56+
}

0 commit comments

Comments
 (0)