-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
丁建水
committed
Oct 16, 2023
1 parent
edf5e4e
commit aa1cea6
Showing
11 changed files
with
357 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
src/main/java/com/wenshuo/agent/applog/CommonsLoggingAppLog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.