Skip to content

Commit

Permalink
支持使用系统引用的日志框架
Browse files Browse the repository at this point in the history
  • Loading branch information
丁建水 committed Oct 16, 2023
1 parent edf5e4e commit aa1cea6
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 97 deletions.
146 changes: 80 additions & 66 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,76 +1,90 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.wenshuo</groupId>
<artifactId>javaagent</artifactId>
<version>2.1.2</version>
<packaging>jar</packaging>
<groupId>com.wenshuo</groupId>
<artifactId>javaagent</artifactId>
<version>2.1.2</version>
<packaging>jar</packaging>

<name>javaagent</name>
<description>基于javaagent技术来记录方法执行次数和时间</description>
<url>https://github.com/dingjs/javaagent</url>
<name>javaagent</name>
<description>基于javaagent技术来记录方法执行次数和时间</description>
<url>https://github.com/dingjs/javaagent</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.target>1.6</project.build.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<developers>
<developer>
<id>dingjsh</id>
<name>丁建水</name>
<email>[email protected]</email>
</developer>
</developers>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.target>1.6</project.build.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<developers>
<developer>
<id>dingjsh</id>
<name>丁建水</name>
<email>[email protected]</email>
</developer>
</developers>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifestEntries>
<premain-class>com.wenshuo.agent.Agent</premain-class>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${project.build.target}</source>
<target>${project.build.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifestEntries>
<premain-class>com.wenshuo.agent.Agent</premain-class>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${project.build.target}</source>
<target>${project.build.target}</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<optional>true</optional>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>compile</scope>
<optional>true</optional>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencies>
</project>
9 changes: 8 additions & 1 deletion src/main/java/com/wenshuo/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
* Copyright 2015 wenshuo, Inc. All rights reserved.
* wenshuo PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package com.wenshuo.agent;

import java.lang.instrument.Instrumentation;

import com.wenshuo.agent.applog.AppLogFactory;
import com.wenshuo.agent.applog.IAppLog;
import com.wenshuo.agent.log.ExecuteLogUtils;
import com.wenshuo.agent.transformer.AgentLogClassFileTransformer;

Expand All @@ -16,10 +21,12 @@
*/
public class Agent {

private static IAppLog log = AppLogFactory.getAppLog(Agent.class);

public static void premain(String agentArs, Instrumentation inst) {
System.out.println("javaagent启动成功,将自动记录方法的执行次数和时间");
// 初始化配置
ConfigUtils.initProperties(agentArs);
log.info("javaagent启动成功,将自动记录方法的执行次数和时间,日志文件路径:" + ConfigUtils.getLogFileName());
ExecuteLogUtils.init();
inst.addTransformer(new AgentLogClassFileTransformer());
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/wenshuo/agent/ConfigUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.util.Properties;
import java.util.Set;

import com.wenshuo.agent.applog.AppLogFactory;
import com.wenshuo.agent.applog.IAppLog;

/**
* 获取配置信息<br>
* ConfigUtils
Expand All @@ -24,6 +27,8 @@ public class ConfigUtils {

private static Set<String> excludeClassRegexs;

private static IAppLog log = AppLogFactory.getAppLog(ConfigUtils.class);

private ConfigUtils() {
super();
}
Expand All @@ -49,7 +54,7 @@ private static Properties getProperties(String propertiesFileName) {
input = ConfigUtils.class.getClassLoader().getResourceAsStream("agent.properties");
properties.load(input);
} catch (Exception e) {
System.err.println("未找到默认配置");
log.warn("未找到默认配置");
} finally {
AgentUtils.closeQuietly(input);
}
Expand All @@ -58,7 +63,7 @@ private static Properties getProperties(String propertiesFileName) {
input = new FileInputStream(propertiesFileName);
properties.load(input);
} catch (Exception e) {
System.err.println(e);
log.error("解析配置文件出错:" + propertiesFileName, e);
} finally {
AgentUtils.closeQuietly(input);
}
Expand Down Expand Up @@ -158,4 +163,5 @@ public static boolean isUsingNanoTime() {
String value = getProperty(ConfigConsts.LOG_TIME_NANO);
return "true".equalsIgnoreCase(value);
}

}
52 changes: 52 additions & 0 deletions src/main/java/com/wenshuo/agent/applog/AppLogFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @projectName javaagent
* @package com.wenshuo.agent.applog
* @className com.wenshuo.agent.applog.IAppLogFactory
* @copyright Copyright 2023 Thunisoft, Inc All rights reserved.
*/

package com.wenshuo.agent.applog;

/**
* AppLogFactory
*
* @author dingjsh
* @description javaagent日志工厂类
* @date 2023-10-16 11:49
* @since 2.1.3
*/
public class AppLogFactory {

private static final String SLF4J_FACTORY_CLASS_NAME = "org.slf4j.LoggerFactory";

private static final String COMMONS_LOGGING_FACTORY_CLASS_NAME = "org.apache.commons.logging.LogFactory";

/**
* 如果工程中有slf4j,则采用slf4j输出日志,如果有commons-logging,则采用commons-logging,否则用console
* @param clazz
* @return javaagent日志对象
*/
public static IAppLog getAppLog(Class<?> clazz) {
if (isClassPresent(SLF4J_FACTORY_CLASS_NAME, Thread.currentThread().getContextClassLoader())) {
return new Slf4jAppLog(clazz);
} else if (isClassPresent(COMMONS_LOGGING_FACTORY_CLASS_NAME, Thread.currentThread().getContextClassLoader())) {
return new CommonsLoggingAppLog(clazz);
} else {
return new ConsoleAppLog();
}
}

protected static boolean isClassPresent(String className, ClassLoader classLoader) {
try {
resolve(className, classLoader);
return true;
} catch (Throwable throwable) {
return false;
}
}

protected static Class<?> resolve(String className, ClassLoader classLoader) throws ClassNotFoundException {
return classLoader != null ? Class.forName(className, false, classLoader) : Class.forName(className);
}

}
50 changes: 50 additions & 0 deletions src/main/java/com/wenshuo/agent/applog/CommonsLoggingAppLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @projectName javaagent
* @package com.wenshuo.agent.applog
* @className com.wenshuo.agent.applog.CommonsLoggingAppLog
* @copyright Copyright 2023 Thunisoft, Inc All rights reserved.
*/

package com.wenshuo.agent.applog;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* CommonsLoggingAppLog
*
* @author dingjsh
* @description
* @date 2023-10-16 17:35
* @since 2.1.3
*/
class CommonsLoggingAppLog implements IAppLog {

private Log log;

public CommonsLoggingAppLog(Class<?> clazz) {
log = LogFactory.getLog(clazz);
}

@Override
public void info(String message) {
log.info(message);

}

@Override
public void warn(String message) {
log.warn(message);
}

@Override
public void error(String message) {
log.error(message);
}

@Override
public void error(String message, Throwable t) {
log.error(message, t);
}

}
40 changes: 40 additions & 0 deletions src/main/java/com/wenshuo/agent/applog/ConsoleAppLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @projectName javaagent
* @package com.wenshuo.agent.applog
* @className com.wenshuo.agent.applog.ConsoleAppLog
* @copyright Copyright 2023 Thunisoft, Inc All rights reserved.
*/

package com.wenshuo.agent.applog;

/**
* ConsoleAppLog
*
* @author dingjsh
* @description
* @date 2023-10-16 17:37
* @since 2.1.3
*/
class ConsoleAppLog implements IAppLog {

@Override
public void info(String message) {
System.out.println("[info ] " + message);
}

@Override
public void warn(String message) {
System.out.println("[warn ] " + message);
}

@Override
public void error(String message) {
System.err.println("[error ] " + message);
}

@Override
public void error(String message, Throwable t) {
System.err.println("[error ] " + message);
}

}
Loading

0 comments on commit aa1cea6

Please sign in to comment.