Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Apache RocketMQ
Rocketmq
提交
98bd0324
R
Rocketmq
项目概览
Apache RocketMQ
/
Rocketmq
上一次同步 大约 3 年
通知
267
Star
16139
Fork
68
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rocketmq
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
98bd0324
编写于
8月 01, 2017
作者:
L
lindzh
提交者:
yukon
8月 01, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Author: lindzh <linsony0@163.com>
Author: 鲁般 <dezhi.ldz@alibaba-inc.com> Closes #120 from lindzh/fix_client_logger.
上级
9bb6eae4
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
158 addition
and
10 deletion
+158
-10
client/pom.xml
client/pom.xml
+10
-0
client/src/main/java/org/apache/rocketmq/client/log/ClientLogger.java
...ain/java/org/apache/rocketmq/client/log/ClientLogger.java
+22
-5
client/src/main/resources/log4j2_rocketmq_client.xml
client/src/main/resources/log4j2_rocketmq_client.xml
+48
-0
client/src/test/java/org/apache/rocketmq/client/log/ClientLogTest.java
...st/java/org/apache/rocketmq/client/log/ClientLogTest.java
+72
-0
example/pom.xml
example/pom.xml
+0
-4
pom.xml
pom.xml
+6
-1
未找到文件。
client/pom.xml
浏览文件 @
98bd0324
...
@@ -45,5 +45,15 @@
...
@@ -45,5 +45,15 @@
<groupId>
org.apache.commons
</groupId>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<artifactId>
commons-lang3
</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-core
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-slf4j-impl
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
client/src/main/java/org/apache/rocketmq/client/log/ClientLogger.java
浏览文件 @
98bd0324
...
@@ -18,6 +18,7 @@ package org.apache.rocketmq.client.log;
...
@@ -18,6 +18,7 @@ package org.apache.rocketmq.client.log;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.net.URL
;
import
java.net.URL
;
import
org.apache.rocketmq.common.constant.LoggerName
;
import
org.apache.rocketmq.common.constant.LoggerName
;
import
org.slf4j.ILoggerFactory
;
import
org.slf4j.ILoggerFactory
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
...
@@ -27,11 +28,10 @@ public class ClientLogger {
...
@@ -27,11 +28,10 @@ public class ClientLogger {
public
static
final
String
CLIENT_LOG_ROOT
=
"rocketmq.client.logRoot"
;
public
static
final
String
CLIENT_LOG_ROOT
=
"rocketmq.client.logRoot"
;
public
static
final
String
CLIENT_LOG_MAXINDEX
=
"rocketmq.client.logFileMaxIndex"
;
public
static
final
String
CLIENT_LOG_MAXINDEX
=
"rocketmq.client.logFileMaxIndex"
;
public
static
final
String
CLIENT_LOG_LEVEL
=
"rocketmq.client.logLevel"
;
public
static
final
String
CLIENT_LOG_LEVEL
=
"rocketmq.client.logLevel"
;
private
static
Logger
log
;
private
static
Logger
log
;
static
{
private
static
Class
logClass
=
null
;
log
=
createLogger
(
LoggerName
.
CLIENT_LOGGER_NAME
);
}
private
static
Logger
createLogger
(
final
String
loggerName
)
{
private
static
Logger
createLogger
(
final
String
loggerName
)
{
String
logConfigFilePath
=
String
logConfigFilePath
=
...
@@ -46,6 +46,9 @@ public class ClientLogger {
...
@@ -46,6 +46,9 @@ public class ClientLogger {
final
String
logbackResourceFile
=
final
String
logbackResourceFile
=
System
.
getProperty
(
"rocketmq.client.logback.resource.fileName"
,
"logback_rocketmq_client.xml"
);
System
.
getProperty
(
"rocketmq.client.logback.resource.fileName"
,
"logback_rocketmq_client.xml"
);
final
String
log4J2ResourceFile
=
System
.
getProperty
(
"rocketmq.client.log4j2.resource.fileName"
,
"log4j2_rocketmq_client.xml"
);
String
clientLogRoot
=
System
.
getProperty
(
CLIENT_LOG_ROOT
,
"${user.home}/logs/rocketmqlogs"
);
String
clientLogRoot
=
System
.
getProperty
(
CLIENT_LOG_ROOT
,
"${user.home}/logs/rocketmqlogs"
);
System
.
setProperty
(
"client.logRoot"
,
clientLogRoot
);
System
.
setProperty
(
"client.logRoot"
,
clientLogRoot
);
String
clientLogLevel
=
System
.
getProperty
(
CLIENT_LOG_LEVEL
,
"INFO"
);
String
clientLogLevel
=
System
.
getProperty
(
CLIENT_LOG_LEVEL
,
"INFO"
);
...
@@ -90,7 +93,16 @@ public class ClientLogger {
...
@@ -90,7 +93,16 @@ public class ClientLogger {
doConfigure
.
invoke
(
joranConfiguratoroObj
,
logConfigFilePath
);
doConfigure
.
invoke
(
joranConfiguratoroObj
,
logConfigFilePath
);
}
}
}
else
if
(
classType
.
getName
().
equals
(
"org.apache.logging.slf4j.Log4jLoggerFactory"
))
{
Class
<?>
joranConfigurator
=
Class
.
forName
(
"org.apache.logging.log4j.core.config.Configurator"
);
Method
initialize
=
joranConfigurator
.
getDeclaredMethod
(
"initialize"
,
String
.
class
,
String
.
class
);
if
(
null
==
logConfigFilePath
)
{
initialize
.
invoke
(
joranConfigurator
,
"log4j2"
,
log4J2ResourceFile
);
}
else
{
initialize
.
invoke
(
joranConfigurator
,
"log4j2"
,
logConfigFilePath
);
}
}
}
logClass
=
classType
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
e
);
System
.
err
.
println
(
e
);
}
}
...
@@ -99,7 +111,12 @@ public class ClientLogger {
...
@@ -99,7 +111,12 @@ public class ClientLogger {
}
}
public
static
Logger
getLog
()
{
public
static
Logger
getLog
()
{
if
(
log
==
null
)
{
log
=
createLogger
(
LoggerName
.
CLIENT_LOGGER_NAME
);
return
log
;
return
log
;
}
else
{
return
log
;
}
}
}
public
static
void
setLog
(
Logger
log
)
{
public
static
void
setLog
(
Logger
log
)
{
...
...
client/src/main/resources/log4j2_rocketmq_client.xml
0 → 100644
浏览文件 @
98bd0324
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE xml>
<Configuration
status=
"warn"
name=
"RocketmqClient"
>
<Appenders>
<Console
name=
"STDOUT-APPENDER"
>
<PatternLayout
pattern=
"%-5p %c{2} , %m%n"
/>
</Console>
<RollingFile
name=
"RocketmqClientAppender"
fileName=
"${sys:client.logRoot}/rocketmq_client.log"
filePattern=
"${sys:client.logRoot}/rocketmq_client-%d{yyyy-MM-dd}-%i.log"
>
<PatternLayout
pattern=
"%d{yyy-MM-dd HH\:mm\:ss,SSS} %p %c{1}(%L) - %m%n"
/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy
size=
"1 GB"
/>
</Policies>
<DefaultRolloverStrategy
max=
"${sys:client.logFileMaxIndex}"
/>
</RollingFile>
</Appenders>
<Loggers>
<logger
name=
"RocketmqClient"
level=
"${sys:client.logLevel}"
additivity=
"false"
>
<appender-ref
ref=
"RocketmqClientAppender"
/>
</logger>
<logger
name=
"RocketmqCommon"
level=
"${sys:client.logLevel}"
additivity=
"false"
>
<appender-ref
ref=
"RocketmqClientAppender"
/>
</logger>
<logger
name=
"RocketmqRemoting"
level=
"${sys:client.logLevel}"
additivity=
"false"
>
<appender-ref
ref=
"RocketmqClientAppender"
/>
</logger>
</Loggers>
</Configuration>
\ No newline at end of file
client/src/test/java/org/apache/rocketmq/client/log/ClientLogTest.java
0 → 100644
浏览文件 @
98bd0324
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.rocketmq.client.log
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
java.io.*
;
import
java.lang.reflect.Field
;
import
java.util.Date
;
public
class
ClientLogTest
{
public
static
final
String
CLIENT_LOG_ROOT
=
"rocketmq.client.logRoot"
;
public
static
final
String
LOG_DIR
;
static
{
LOG_DIR
=
System
.
getProperty
(
CLIENT_LOG_ROOT
,
"${user.home}/logs/rocketmqlogs"
);
}
// FIXME: Workarond for concret implementation for slf4j, is there any better solution for all slf4j implementations in one class ? 2017/8/1
@Test
public
void
testLog4j2
()
throws
IOException
,
NoSuchFieldException
,
IllegalAccessException
{
ClientLogger
.
getLog
();
long
seek
=
0
;
boolean
result
=
false
;
File
file
=
new
File
(
LOG_DIR
+
File
.
separator
+
"rocketmq_client.log"
);
if
(
file
.
exists
())
{
seek
=
file
.
length
();
}
Field
logClassField
=
ClientLogger
.
class
.
getDeclaredField
(
"logClass"
);
logClassField
.
setAccessible
(
true
);
Class
logClass
=
(
Class
)
logClassField
.
get
(
ClientLogger
.
class
);
Assert
.
assertEquals
(
"org.apache.logging.slf4j.Log4jLoggerFactory"
,
logClass
.
getName
());
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
ClientLogger
.
getLog
().
info
(
"testcase testLog4j2 "
+
new
Date
());
}
RandomAccessFile
randomAccessFile
=
new
RandomAccessFile
(
file
,
"r"
);
randomAccessFile
.
seek
(
seek
);
String
line
=
randomAccessFile
.
readLine
();
int
idx
=
1
;
while
(
line
!=
null
)
{
if
(
line
.
contains
(
"testLog4j2"
))
{
result
=
true
;
break
;
}
line
=
randomAccessFile
.
readLine
();
idx
++;
if
(
idx
>
20
)
{
break
;
}
}
randomAccessFile
.
close
();
Assert
.
assertTrue
(
result
);
}
}
example/pom.xml
浏览文件 @
98bd0324
...
@@ -40,10 +40,6 @@
...
@@ -40,10 +40,6 @@
<groupId>
ch.qos.logback
</groupId>
<groupId>
ch.qos.logback
</groupId>
<artifactId>
logback-classic
</artifactId>
<artifactId>
logback-classic
</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>
ch.qos.logback
</groupId>
<artifactId>
logback-core
</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>
org.javassist
</groupId>
<groupId>
org.javassist
</groupId>
<artifactId>
javassist
</artifactId>
<artifactId>
javassist
</artifactId>
...
...
pom.xml
浏览文件 @
98bd0324
...
@@ -530,7 +530,7 @@
...
@@ -530,7 +530,7 @@
<dependency>
<dependency>
<groupId>
org.slf4j
</groupId>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-api
</artifactId>
<artifactId>
slf4j-api
</artifactId>
<version>
1.7.
5
</version>
<version>
1.7.
7
</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
ch.qos.logback
</groupId>
<groupId>
ch.qos.logback
</groupId>
...
@@ -592,6 +592,11 @@
...
@@ -592,6 +592,11 @@
<artifactId>
log4j-core
</artifactId>
<artifactId>
log4j-core
</artifactId>
<version>
2.7
</version>
<version>
2.7
</version>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-slf4j-impl
</artifactId>
<version>
2.7
</version>
</dependency>
</dependencies>
</dependencies>
</dependencyManagement>
</dependencyManagement>
</project>
</project>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录