-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for finding the console's charset, LOGBACK-1642
Signed-off-by: Ceki Gulcu <[email protected]>
- Loading branch information
Showing
8 changed files
with
131 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
~ Logback: the reliable, generic, fast and flexible logging framework. | ||
~ Copyright (C) 1999-2024, QOS.ch. All rights reserved. | ||
~ | ||
~ This program and the accompanying materials are dual-licensed under | ||
~ either the terms of the Eclipse Public License v1.0 as published by | ||
~ the Eclipse Foundation | ||
~ | ||
~ or (per the licensee's choosing) | ||
~ | ||
~ under the terms of the GNU Lesser General Public License version 2.1 | ||
~ as published by the Free Software Foundation. | ||
--> | ||
|
||
<!DOCTYPE configuration> | ||
|
||
<configuration> | ||
<import class="ch.qos.logback.core.status.OnConsoleStatusListener"/> | ||
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/> | ||
<import class="ch.qos.logback.core.ConsoleAppender"/> | ||
<import class="ch.qos.logback.core.property.ConsoleCharsetPropertyDefiner"/> | ||
|
||
<define name="consoleCharset" class="ConsoleCharsetPropertyDefiner"/> | ||
|
||
<appender name="CON" class="ConsoleAppender"> | ||
<encoder class="PatternLayoutEncoder"> | ||
<charset>${consoleCharset}</charset> | ||
<pattern>TEST %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root> | ||
<level value="DEBUG"/> | ||
<appender-ref ref="CON"/> | ||
</root> | ||
<statusListener class="OnConsoleStatusListener"/> | ||
</configuration> |
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
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
49 changes: 49 additions & 0 deletions
49
logback-core/src/main/java21/ch/qos/logback/core/property/ConsoleCharsetPropertyDefiner.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,49 @@ | ||
/* | ||
* Logback: the reliable, generic, fast and flexible logging framework. | ||
* Copyright (C) 1999-2024, QOS.ch. All rights reserved. | ||
* | ||
* This program and the accompanying materials are dual-licensed under | ||
* either the terms of the Eclipse Public License v1.0 as published by | ||
* the Eclipse Foundation | ||
* | ||
* or (per the licensee's choosing) | ||
* | ||
* under the terms of the GNU Lesser General Public License version 2.1 | ||
* as published by the Free Software Foundation. | ||
*/ | ||
|
||
package ch.qos.logback.core.property; | ||
|
||
import ch.qos.logback.core.PropertyDefinerBase; | ||
import static ch.qos.logback.core.CoreConstants.NULL_STR; | ||
|
||
import java.io.Console; | ||
import java.nio.charset.Charset; | ||
|
||
/** | ||
* Compute the console's charset. | ||
* | ||
* @since 1.5.7 | ||
*/ | ||
public class ConsoleCharsetPropertyDefiner extends PropertyDefinerBase { | ||
@Override | ||
public String getPropertyValue() { | ||
// System.console().charset() requires Java 17 | ||
Console console = System.console(); | ||
if (console != null) { | ||
Charset charset = console.charset(); | ||
if (charset != null) { | ||
String charsetName = charset.name(); | ||
addInfo("Found value '" + charsetName + "' as returned by System.console()."); | ||
return charsetName; | ||
} else { | ||
addInfo("System.console() returned null charset. Returning \"NULL\" string as defined value."); | ||
return NULL_STR; | ||
} | ||
} else { | ||
addWarn("System.console() returned null. Cannot compute console's charset, returning the \"NULL\" string, i.e. the default JVM charset"); | ||
return NULL_STR; | ||
} | ||
} | ||
|
||
} |