Skip to content

Commit 7f37a07

Browse files
committed
android logger simple implementation
1 parent 4eb26ef commit 7f37a07

File tree

7 files changed

+253
-0
lines changed

7 files changed

+253
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ buildNumber.properties
99
.mvn/timing.properties
1010
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
1111
.mvn/wrapper/maven-wrapper.jar
12+
.idea/
13+
log4j2-android.iml

pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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>com.celeral</groupId>
9+
<version>2.1.3</version>
10+
<artifactId>parent</artifactId>
11+
</parent>
12+
13+
<groupId>com.celeral</groupId>
14+
<artifactId>log4j2-android</artifactId>
15+
<version>1.0.0</version>
16+
17+
<name>log4j2-android</name>
18+
19+
<properties>
20+
<revision>1.0.0</revision>
21+
<revision.base>NULL</revision.base>
22+
</properties>
23+
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-jar-plugin</artifactId>
29+
<version>3.2.0</version>
30+
<configuration>
31+
<excludes>
32+
<exclude>android/**</exclude>
33+
</excludes>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
<profiles>
39+
<profile>
40+
<id>release</id>
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.codehaus.mojo</groupId>
45+
<artifactId>flatten-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</profile>
50+
</profiles>
51+
52+
<dependencies>
53+
<dependency>
54+
<groupId>org.apache.logging.log4j</groupId>
55+
<artifactId>log4j-api</artifactId>
56+
</dependency>
57+
</dependencies>
58+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package android.util;
2+
3+
public class Log
4+
{
5+
static final UnsupportedOperationException exception = new UnsupportedOperationException("This is a stub; Actual Implementation will be auto invoked on Android platform!");
6+
7+
public static int v(String tag, String formattedMessage, Throwable throwable)
8+
{
9+
throw exception;
10+
}
11+
12+
public static int d(String tag, String formattedMessage, Throwable throwable)
13+
{
14+
throw exception;
15+
}
16+
17+
public static int i(String tag, String formattedMessage, Throwable throwable)
18+
{
19+
throw exception;
20+
}
21+
22+
public static int w(String tag, String formattedMessage, Throwable throwable)
23+
{
24+
throw exception;
25+
}
26+
27+
public static int e(String tag, String formattedMessage, Throwable throwable)
28+
{
29+
throw exception;
30+
}
31+
32+
public static int wtf(String tag, String formattedMessage, Throwable throwable)
33+
{
34+
throw exception;
35+
}
36+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright © 2020 Celeral. */
2+
package com.celeral.android.logging.log4j;
3+
4+
import android.util.Log;
5+
6+
import org.apache.logging.log4j.Level;
7+
import org.apache.logging.log4j.Marker;
8+
import org.apache.logging.log4j.message.Message;
9+
import org.apache.logging.log4j.message.MessageFactory;
10+
import org.apache.logging.log4j.simple.SimpleLogger;
11+
import org.apache.logging.log4j.util.PropertiesUtil;
12+
13+
public class AndroidLogger extends SimpleLogger {
14+
final String tag;
15+
16+
public AndroidLogger(String name, Level defaultLevel, MessageFactory messageFactory) {
17+
super(
18+
name,
19+
defaultLevel,
20+
false,
21+
false,
22+
false,
23+
false,
24+
null,
25+
messageFactory,
26+
PropertiesUtil.getProperties(),
27+
null);
28+
29+
if (name == null) {
30+
tag = null;
31+
} else {
32+
int lastIndex = name.lastIndexOf('.');
33+
tag = lastIndex == -1 ? name : name.substring(lastIndex + 1);
34+
}
35+
}
36+
37+
@Override
38+
public void logMessage(
39+
String fqcn, Level mgsLevel, Marker marker, Message msg, Throwable throwable) {
40+
switch (mgsLevel.getStandardLevel()) {
41+
case ALL:
42+
case TRACE:
43+
Log.v(tag, msg.getFormattedMessage(), throwable);
44+
break;
45+
46+
case DEBUG:
47+
Log.d(tag, msg.getFormattedMessage(), throwable);
48+
break;
49+
50+
case INFO:
51+
Log.i(tag, msg.getFormattedMessage(), throwable);
52+
break;
53+
54+
case WARN:
55+
Log.w(tag, msg.getFormattedMessage(), throwable);
56+
break;
57+
58+
case ERROR:
59+
Log.e(tag, msg.getFormattedMessage(), throwable);
60+
break;
61+
62+
case FATAL:
63+
Log.wtf(tag, msg.getFormattedMessage(), throwable);
64+
break;
65+
66+
case OFF:
67+
default:
68+
break;
69+
}
70+
}
71+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright © 2020 Celeral. */
2+
package com.celeral.android.logging.log4j;
3+
4+
import org.apache.logging.log4j.Level;
5+
import org.apache.logging.log4j.message.MessageFactory;
6+
import org.apache.logging.log4j.spi.AbstractLogger;
7+
import org.apache.logging.log4j.spi.ExtendedLogger;
8+
import org.apache.logging.log4j.spi.LoggerContext;
9+
import org.apache.logging.log4j.spi.LoggerRegistry;
10+
11+
public class AndroidLoggerContext implements LoggerContext {
12+
private final LoggerRegistry<ExtendedLogger> loggerRegistry = new LoggerRegistry<>();
13+
14+
@Override
15+
public Object getExternalContext() {
16+
return null;
17+
}
18+
19+
@Override
20+
public ExtendedLogger getLogger(String name) {
21+
return getLogger(name, null);
22+
}
23+
24+
@Override
25+
public ExtendedLogger getLogger(String name, MessageFactory messageFactory) {
26+
final ExtendedLogger extendedLogger = loggerRegistry.getLogger(name, messageFactory);
27+
if (extendedLogger != null) {
28+
AbstractLogger.checkMessageFactory(extendedLogger, messageFactory);
29+
return extendedLogger;
30+
}
31+
32+
final AndroidLogger logger = new AndroidLogger(name, Level.ALL, messageFactory);
33+
34+
loggerRegistry.putIfAbsent(name, messageFactory, logger);
35+
return loggerRegistry.getLogger(name, messageFactory);
36+
}
37+
38+
@Override
39+
public boolean hasLogger(String name) {
40+
return false;
41+
}
42+
43+
@Override
44+
public boolean hasLogger(String name, MessageFactory messageFactory) {
45+
return false;
46+
}
47+
48+
@Override
49+
public boolean hasLogger(String name, Class<? extends MessageFactory> messageFactoryClass) {
50+
return false;
51+
}
52+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright © 2020 Celeral. */
2+
package com.celeral.android.logging.log4j;
3+
4+
import java.net.URI;
5+
6+
import org.apache.logging.log4j.spi.LoggerContext;
7+
import org.apache.logging.log4j.spi.LoggerContextFactory;
8+
9+
public class AndroidLoggerContextFactory implements LoggerContextFactory {
10+
private static LoggerContext context = new AndroidLoggerContext();
11+
12+
@Override
13+
public LoggerContext getContext(
14+
String fqcn, ClassLoader loader, Object externalContext, boolean currentContext) {
15+
return context;
16+
}
17+
18+
@Override
19+
public LoggerContext getContext(
20+
String fqcn,
21+
ClassLoader loader,
22+
Object externalContext,
23+
boolean currentContext,
24+
URI configLocation,
25+
String name) {
26+
return context;
27+
}
28+
29+
@Override
30+
public void removeContext(LoggerContext context) {
31+
/* there is nothing for us to remove! */
32+
}
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
log4j2.loggerContextFactory=com.celeral.android.logging.log4j.AndroidLoggerContextFactory

0 commit comments

Comments
 (0)