Skip to content

Commit

Permalink
Use system standard output stream for console loggers when running in…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
JamesChenX committed May 25, 2024
1 parent a9aa281 commit 5d6a23d
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class ApplicationEnvironmentEventListener

/**
* @implNote We don't use {@link ApplicationContextInitializedEvent} because it's still too late
* for logging
* for logging.
*/
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
Expand Down Expand Up @@ -145,7 +145,7 @@ private void configureContextForLogging(ApplicationEnvironmentPreparedEvent even
.file(fileLoggingProperties)
.build();

LoggerFactory.init(nodeType, Node.getNodeId(), loggingProperties);
LoggerFactory.init(false, nodeType, Node.getNodeId(), loggingProperties);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@

package im.turms.server.common.infra.logging.core.appender;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import io.netty.buffer.ByteBuf;
import lombok.Data;

import im.turms.server.common.infra.io.InputOutputException;
import im.turms.server.common.infra.logging.core.model.LogLevel;
import im.turms.server.common.infra.logging.core.model.LogRecord;

Expand All @@ -34,58 +28,15 @@
@Data
public abstract class Appender implements AutoCloseable {

private final LogLevel level;

protected FileChannel channel;
protected final LogLevel level;

protected Appender(LogLevel level) {
this.level = level;
}

@Override
public void close() {
try {
channel.force(true);
} catch (IOException e) {
throw new InputOutputException(
"Caught an error while forcing updates to the channel's file to be written",
e);
}
try {
channel.close();
} catch (IOException e) {
throw new InputOutputException("Caught an error while closing the channel", e);
}
}

public int append(LogRecord record) {
if (!record.level()
.isLoggable(level)) {
return 0;
}
ByteBuf buffer = record.data();
if (buffer.nioBufferCount() == 1) {
try {
return channel.write(buffer.nioBuffer());
} catch (IOException e) {
throw new InputOutputException(
"Failed to write the buffer: "
+ buffer,
e);
}
}
int written = 0;
for (ByteBuffer buf : buffer.nioBuffers()) {
try {
written += channel.write(buf);
} catch (IOException e) {
throw new InputOutputException(
"Failed to write the buffer: "
+ buffer,
e);
}
}
return written;
public void close() throws Exception {
}

}
public abstract int append(LogRecord record);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* Licensed 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 im.turms.server.common.infra.logging.core.appender;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import io.netty.buffer.ByteBuf;
import lombok.Data;

import im.turms.server.common.infra.io.InputOutputException;
import im.turms.server.common.infra.logging.core.model.LogLevel;
import im.turms.server.common.infra.logging.core.model.LogRecord;

/**
* @author James Chen
*/
@Data
public abstract class ChannelAppender extends Appender {

protected FileChannel channel;

protected ChannelAppender(LogLevel level) {
super(level);
}

@Override
public void close() {
try {
channel.force(true);
} catch (IOException e) {
throw new InputOutputException(
"Caught an error while forcing updates to the channel's file to be written",
e);
}
try {
channel.close();
} catch (IOException e) {
throw new InputOutputException("Caught an error while closing the channel", e);
}
}

@Override
public int append(LogRecord record) {
if (!record.level()
.isLoggable(level)) {
return 0;
}
ByteBuf buffer = record.data();
if (buffer.nioBufferCount() == 1) {
try {
return channel.write(buffer.nioBuffer());
} catch (IOException e) {
throw new InputOutputException(
"Failed to write the buffer: "
+ buffer,
e);
}
}
int written = 0;
for (ByteBuffer buf : buffer.nioBuffers()) {
try {
written += channel.write(buf);
} catch (IOException e) {
throw new InputOutputException(
"Failed to write the buffer: "
+ buffer,
e);
}
}
return written;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
/**
* @author James Chen
*/
public class ConsoleAppender extends Appender {
public class ChannelConsoleAppender extends ChannelAppender {

public ConsoleAppender(LogLevel level) {
public ChannelConsoleAppender(LogLevel level) {
super(level);
channel = new FileOutputStream(FileDescriptor.out).getChannel();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* Licensed 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 im.turms.server.common.infra.logging.core.appender;

import im.turms.server.common.infra.logging.core.model.LogLevel;
import im.turms.server.common.infra.logging.core.model.LogRecord;
import im.turms.server.common.infra.netty.ByteBufUtil;

public class SystemConsoleAppender extends Appender {

public SystemConsoleAppender(LogLevel level) {
super(level);
}

@Override
public int append(LogRecord record) {
if (!record.level()
.isLoggable(level)) {
return 0;
}
String s = ByteBufUtil.getString(record.data());
if (record.level()
.isErrorOrFatal()) {
System.err.println(s);
} else {
System.out.println(s);
}
return record.data()
.readableBytes();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

import im.turms.server.common.infra.io.InputOutputException;
import im.turms.server.common.infra.lang.StringUtil;
import im.turms.server.common.infra.logging.core.appender.Appender;
import im.turms.server.common.infra.logging.core.appender.ChannelAppender;
import im.turms.server.common.infra.logging.core.compression.FastGzipOutputStream;
import im.turms.server.common.infra.logging.core.logger.InternalLogger;
import im.turms.server.common.infra.logging.core.model.LogLevel;
Expand All @@ -51,7 +51,7 @@
/**
* @author James Chen
*/
public class RollingFileAppender extends Appender {
public class RollingFileAppender extends ChannelAppender {

public static final char FIELD_DELIMITER = '_';
private static final String FILE_MIDDLE = "yyyyMMdd";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import im.turms.server.common.infra.lang.ClassUtil;
import im.turms.server.common.infra.lang.Pair;
import im.turms.server.common.infra.logging.core.appender.Appender;
import im.turms.server.common.infra.logging.core.appender.ConsoleAppender;
import im.turms.server.common.infra.logging.core.appender.ChannelConsoleAppender;
import im.turms.server.common.infra.logging.core.appender.SystemConsoleAppender;
import im.turms.server.common.infra.logging.core.appender.file.RollingFileAppender;
import im.turms.server.common.infra.logging.core.layout.TurmsTemplateLayout;
import im.turms.server.common.infra.logging.core.model.LogLevel;
Expand Down Expand Up @@ -70,14 +71,15 @@ public class LoggerFactory {
private static String homeDir;
private static String serverTypeName;
private static FileLoggingProperties fileLoggingProperties;
private static ConsoleAppender defaultConsoleAppender;
private static Appender defaultConsoleAppender;

private static LogProcessor processor;

private LoggerFactory() {
}

public static synchronized void init(
boolean runWithTests,
@Nullable NodeType nodeType,
String nodeId,
LoggingProperties properties) {
Expand All @@ -100,8 +102,13 @@ public static synchronized void init(
ConsoleLoggingProperties consoleLoggingProperties = properties.getConsole();
FileLoggingProperties fileLoggingProperties = properties.getFile();
if (consoleLoggingProperties.isEnabled()) {
ConsoleAppender consoleAppender =
new ConsoleAppender(consoleLoggingProperties.getLevel());
Appender consoleAppender = runWithTests
// Surefire will replace the default System.out and System.err
// with org.apache.maven.surefire.api.report.ConsoleOutputCapture,
// so we use SystemConsoleAppender instead of ChannelConsoleAppender
// for tests.
? new SystemConsoleAppender(consoleLoggingProperties.getLevel())
: new ChannelConsoleAppender(consoleLoggingProperties.getLevel());
defaultConsoleAppender = consoleAppender;
DEFAULT_APPENDERS.add(consoleAppender);
}
Expand Down Expand Up @@ -154,7 +161,8 @@ private static synchronized void initForTest() {
// while our logger will require Netty to init so that we can log, so there is a circular
// dependency.
// Use "INFO" can just avoid Netty trying to log when initializing
init(nodeType,
init(true,
nodeType,
"test",
LoggingProperties.builder()
.console(new ConsoleLoggingProperties().toBuilder()
Expand Down Expand Up @@ -256,4 +264,4 @@ private static boolean isJUnitTest() {
return false;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public boolean isLoggable(LogLevel enabledLevel) {
return enabledLevel.ordinal() <= ordinal();
}

}
public boolean isErrorOrFatal() {
return this == ERROR || this == FATAL;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import im.turms.server.common.infra.lang.NumberFormatter;
import im.turms.server.common.infra.lang.StringUtil;
import im.turms.server.common.infra.test.VisibleForTesting;

/**
* @author James Chen
Expand Down Expand Up @@ -149,7 +148,6 @@ public static String readString(ByteBuf buffer) {
return new String(bytes, StandardCharsets.UTF_8);
}

@VisibleForTesting
public static String getString(ByteBuf buffer) {
buffer.markReaderIndex();
byte[] bytes = new byte[buffer.readableBytes()];
Expand Down

0 comments on commit 5d6a23d

Please sign in to comment.