You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2015/04/16 08:11:59 UTC

[jira] [Commented] (LOG4J2-965) Log4j2 cause System.out unable to work on windows

    [ https://issues.apache.org/jira/browse/LOG4J2-965?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14497608#comment-14497608 ] 

ASF GitHub Bot commented on LOG4J2-965:
---------------------------------------

GitHub user thiagokronig opened a pull request:

    https://github.com/apache/logging-log4j2/pull/10

    LOG4J2-965 Jansi: Fixes unnecessary closing of System.out

    When WindowsAnsiOutputStream is on the classpath, we wrap both System.out
    and System.err with it and pass it to the OutputStreamManager, which upon
    close, tries to close its underlying stream.
    
    Here we solve this by wrapping WindowsAnsiOutputStream with a
    NoCloseOutputStream, effectively ignoring the OutputStreamManager close.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/thiagokronig/logging-log4j2 LOG4J2-965

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/logging-log4j2/pull/10.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #10
    
----
commit 76cff8f8b7b79d1d13d9924582fff43f86d0b42c
Author: Thiago Kronig <th...@gmail.com>
Date:   2015-04-16T06:02:43Z

    LOG4J2-965 Jansi: Fixes unnecessary closing of System.out
    
    When WindowsAnsiOutputStream is on the classpath, we wrap both System.out
    and System.err with it and pass it to the OutputStreamManager, which upon
    close, tries to close its underlying stream.
    
    Here we solve this by wrapping WindowsAnsiOutputStream with a
    NoCloseOutputStream, effectively ignoring the OutputStreamManager close.

----


> Log4j2 cause System.out unable to work on windows
> -------------------------------------------------
>
>                 Key: LOG4J2-965
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-965
>             Project: Log4j 2
>          Issue Type: Bug
>    Affects Versions: 2.1, 2.2
>         Environment: Windows, with jansi  in classpath
>            Reporter: Khotyn Huang
>            Priority: Critical
>
> h3. Demonstration
> The underlining project demonstrate the bug.
> The project's build.gradle file:
> {code:title=build.gradle}
> apply plugin: 'java'
> version = '1.0'
> repositories {
>     mavenCentral()
> }
> def log4j2Version = '2.2'
> def log4j2GroupId = "org.apache.logging.log4j"
> dependencies {
>     compile log4j2GroupId + ':log4j-core:' + log4j2Version
>     compile log4j2GroupId + ":log4j-jcl:" + log4j2Version
>     compile log4j2GroupId + ":log4j-slf4j-impl:" + log4j2Version
>     compile 'org.fusesource.jansi:jansi:1.11'
> }
> {code}
> A log4j2.xml in classpath:
> {code:title=log4j2.xml}
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration status="WARN">
>     <Appenders>
>         <File name="root" fileName="${sys:user.home}/logs/windowsbug.log">
>             <PatternLayout>
>                 <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
>             </PatternLayout>
>         </File>
>     </Appenders>
>     <Loggers>
>         <Root level="info">
>             <AppenderRef ref="root"/>
>         </Root>
>     </Loggers>
> </Configuration>
> {code}
> And the main class:
> {code:title=Log4j2WindowsBug.java}
> import org.slf4j.LoggerFactory;
> /**
>  * @author khotyn 15/3/2 下午8:17
>  */
> public class Log4j2WindowsBug {
>     public static void main(String[] args) {
>         System.out.println("Able to print on Windows");
>         LoggerFactory.getLogger(Log4j2WindowsBug.class);
>         System.out.println("Unable to print on Windows");
>     }
> }
> {code}
> The output of the demo under Windows is:
> {code}
> Able to print on Windows
> {code}
> The third line did not print to Windows console.
> h3. Reason
> It seems that log4j2 will wrapper System.out to WindowsAnsiOutputStream if jansi is available in classpath. And in OutputStreamManager's close method, the wrapper WindowsAnsiOutputStream is not considered, and cause the underling System.out closed.
> {code:title=OutputStreamManager.java}
>     protected synchronized void close() {
>         final OutputStream stream = os; // access volatile field only once per method
>         if (stream == System.out || stream == System.err) {
>             return;
>         }
>         try {
>             stream.close();
>         } catch (final IOException ex) {
>             LOGGER.error("Unable to close stream " + getName() + ". " + ex);
>         }
>     }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org