-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use system standard output stream for console loggers when running in…
… tests
- Loading branch information
1 parent
a9aa281
commit 5d6a23d
Showing
9 changed files
with
166 additions
and
68 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
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
90 changes: 90 additions & 0 deletions
90
...mon/src/main/java/im/turms/server/common/infra/logging/core/appender/ChannelAppender.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,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; | ||
} | ||
|
||
} |
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
47 changes: 47 additions & 0 deletions
47
...c/main/java/im/turms/server/common/infra/logging/core/appender/SystemConsoleAppender.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,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(); | ||
} | ||
|
||
} |
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
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