Skip to content

Commit

Permalink
feat(logging): apache#13378 Uniformly uses log4j2 as the logging comp…
Browse files Browse the repository at this point in the history
…onent
  • Loading branch information
oxsean committed Nov 21, 2023
1 parent 650ee3b commit 18e363a
Show file tree
Hide file tree
Showing 199 changed files with 2,956 additions and 798 deletions.
4 changes: 2 additions & 2 deletions dubbo-cluster/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.logger.Level;
import org.apache.dubbo.common.utils.DubboAppender;
import org.apache.dubbo.common.utils.LogUtil;
import org.apache.dubbo.rpc.AppResponse;
Expand All @@ -34,7 +35,6 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Level;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down
38 changes: 0 additions & 38 deletions dubbo-cluster/src/test/resources/log4j.xml

This file was deleted.

33 changes: 33 additions & 0 deletions dubbo-cluster/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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.
-->
<Configuration status="WARN">
<Appenders>
<Dubbo name="Dubbo" fileName="../dubbo.log">
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] %5p %t %c{2}: %m%n%ex" charset="UTF-8"/>
</Dubbo>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} |-%highlight{%-5p} [%t] %40.40c:%-3L -| %m%n%rEx{filters(jdk.internal.reflect,java.lang.reflect,sun.reflect,org.junit,org.mockito)}" charset="UTF-8"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Dubbo"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
4 changes: 2 additions & 2 deletions dubbo-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,91 @@
*/
package org.apache.dubbo.common.utils;

import org.apache.dubbo.common.logger.Level;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.FileAppender;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;

@Plugin(name = "Dubbo", category = "Core", elementType = "appender")
public class DubboAppender extends AbstractAppender {

public static class Builder extends AbstractOutputStreamAppender.Builder<Builder>
implements org.apache.logging.log4j.core.util.Builder<DubboAppender> {

@PluginBuilderAttribute
private String fileName;

@PluginBuilderAttribute
private boolean append = true;

public class DubboAppender extends FileAppender {
@PluginBuilderAttribute
private boolean locking;

public Builder setFileName(String fileName) {
this.fileName = fileName;
return this;
}

public Builder setAppend(boolean append) {
this.append = append;
return this;
}

public Builder setLocking(boolean locking) {
this.locking = locking;
return this;
}

@Override
public DubboAppender build() {
return new DubboAppender(getName(), buildFileAppender());
}

private <B extends FileAppender.Builder<B>> FileAppender buildFileAppender() {
FileAppender.Builder<B> builder = FileAppender.newBuilder();
builder.setIgnoreExceptions(isIgnoreExceptions());
builder.setLayout(getLayout());
builder.setName(getName() + "-File");
builder.setConfiguration(getConfiguration());
builder.setBufferedIo(isBufferedIo());
builder.setBufferSize(getBufferSize());
builder.setImmediateFlush(isImmediateFlush());
builder.withFileName(fileName == null || fileName.isEmpty() ? DEFAULT_FILE_NAME : fileName);
builder.withAppend(append);
builder.withLocking(locking);
return builder.build();
}
}

private static final String DEFAULT_FILE_NAME = "dubbo.log";

public static boolean available = false;
public static List<Log> logList = new ArrayList<>();

private final FileAppender fileAppender;

public DubboAppender() {
super();
setFile(DEFAULT_FILE_NAME);
this("Dubbo", null);
}

public static boolean available = false;
private DubboAppender(String name, FileAppender fileAppender) {
super(name, null, null, true, Property.EMPTY_ARRAY);
this.fileAppender = fileAppender;
}

public static List<Log> logList = new ArrayList<>();
@PluginBuilderFactory
public static Builder newBuilder() {
return new Builder().asBuilder();
}

public static void doStart() {
available = true;
Expand All @@ -48,20 +115,39 @@ public static void clear() {
}

@Override
public void append(LoggingEvent event) {
super.append(event);
public void append(LogEvent event) {
if (fileAppender != null) {
fileAppender.append(event);
}
if (available) {
Log temp = parseLog(event);
logList.add(temp);
logList.add(parseLog(event));
}
}

private Log parseLog(LoggingEvent event) {
@Override
public void initialize() {
fileAppender.initialize();
super.initialize();
}

@Override
public void start() {
fileAppender.start();
super.start();
}

@Override
public void stop() {
super.stop();
fileAppender.stop();
}

private Log parseLog(LogEvent event) {
Log log = new Log();
log.setLogName(event.getLogger().getName());
log.setLogLevel(event.getLevel());
log.setLogName(event.getLoggerName());
log.setLogLevel(Level.valueOf(event.getLevel().name()));
log.setLogThread(event.getThreadName());
log.setLogMessage(event.getMessage().toString());
log.setLogMessage(event.getMessage().getFormattedMessage());
return log;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package org.apache.dubbo.common.utils;

import java.io.Serializable;
import org.apache.dubbo.common.logger.Level;

import org.apache.log4j.Level;
import java.io.Serializable;

public class Log implements Serializable {
private static final long serialVersionUID = -534113138054377073L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
*/
package org.apache.dubbo.common.utils;

import org.apache.dubbo.common.logger.Level;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Level;

public class LogUtil {

private static final Logger Log = LoggerFactory.getLogger(LogUtil.class);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ void testCacheContains() throws URISyntaxException {
}

private String getDirectoryOfClassPath() throws URISyntaxException {
URL resource = this.getClass().getResource("/log4j.xml");
URL resource = this.getClass().getResource("/log4j2-test.xml");
String path = Paths.get(resource.toURI()).toFile().getAbsolutePath();
int index = path.indexOf("log4j.xml");
int index = path.indexOf("log4j2-test.xml");
String directoryPath = path.substring(0, index);
return directoryPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ void testFileSizeExceed() throws Exception {
}

private String getDirectoryOfClassPath() throws URISyntaxException {
URL resource = this.getClass().getResource("/log4j.xml");
URL resource = this.getClass().getResource("/log4j2-test.xml");
String path = Paths.get(resource.toURI()).toFile().getAbsolutePath();
int index = path.indexOf("log4j.xml");
int index = path.indexOf("log4j2-test.xml");
String directoryPath = path.substring(0, index);
return directoryPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package org.apache.dubbo.common.utils;

import org.apache.log4j.Category;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.message.SimpleMessage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -33,15 +33,15 @@
import static org.mockito.Mockito.when;

class DubboAppenderTest {
private LoggingEvent event;
private Log4jLogEvent event;

@BeforeEach
public void setUp() throws Exception {
event = mock(LoggingEvent.class);
when(event.getLogger()).thenReturn(mock(Category.class));
when(event.getLevel()).thenReturn(mock(Level.class));
event = mock(Log4jLogEvent.class);
when(event.getLoggerName()).thenReturn("logger-name");
when(event.getLevel()).thenReturn(Level.INFO);
when(event.getThreadName()).thenReturn("thread-name");
when(event.getMessage()).thenReturn("message");
when(event.getMessage()).thenReturn(new SimpleMessage("message"));
}

@AfterEach
Expand All @@ -63,7 +63,7 @@ void testAvailable() {
void testAppend() {
DubboAppender appender = new DubboAppender();
appender.append(event);
assumeTrue(0 == DubboAppender.logList.size());
assumeTrue(DubboAppender.logList.isEmpty());
DubboAppender.doStart();
appender.append(event);
assertThat(DubboAppender.logList, hasSize(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
package org.apache.dubbo.common.utils;

import org.apache.log4j.Level;
import org.apache.dubbo.common.logger.Level;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
package org.apache.dubbo.common.utils;

import org.apache.log4j.Level;
import org.apache.dubbo.common.logger.Level;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

Expand Down
Loading

0 comments on commit 18e363a

Please sign in to comment.