Java log4j 写入控制台但不写入文件(Liferay + Tomcat)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18640215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:46:21  来源:igfitidea点击:

log4j writing to console but not to file (Liferay + Tomcat)

javatomcatlogginglog4jliferay

提问by Ben VonDerHaar

I've recently had issues with my Liferay/Tomcat logs getting clogged up from several portlets logging to the same files, which makes it tough to track down issues sometimes. Decided I'd like to have a log file for each portlet so it is easier to track down issues and I've found some helpful articles, but no matter what I try I cannot get the custom log file to be created (and by extension written to). As per this article, I've added the following lines to liferay-plugin-package.properties:

我最近遇到了 Liferay/Tomcat 日志被多个 portlet 记录到同一个文件所阻塞的问题,这使得有时很难追踪问题。决定我想要为每个 portlet 创建一个日志文件,以便更容易地跟踪问题,我找到了一些有用的文章,但无论我尝试什么,我都无法创建自定义日志文件(以及扩展名)写到)。根据这篇文章,我在 liferay-plugin-package.properties 中添加了以下几行:

portal-dependency-jars=\
    log4j.jar,\
    log4j-extras.jar

And my log4j.xml looks something like this:

我的 log4j.xml 看起来像这样:

<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}:%L] %m%n" />    
        </layout>
    </appender>

    <appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">

        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
            <param name="FileNamePattern" value="../../logs/portrait-manager-portlet.%d{yyyy-MM-dd}.log" />
        </rollingPolicy>

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}:%L] %m%n" />
        </layout>
    </appender>

    <!-- test is a legitimate package name in my code -->
    <category name="com.test" >
        <priority value="DEBUG" />
    </category>

    <root>
        <priority value="INFO" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</log4j:configuration>

And lastly, logging is done in this manner, as per the article:

最后,根据文章,以这种方式完成日志记录:

private static Logger log = Logger.getLogger(PortraitManagerServiceImpl.class);
log.info("testing log4j logging");

Again, as mentioned, the console logging works just fine but the log file is never created so it can't be written to. If anyone has any suggestions or insight to this behavior I would be most appreciative if they could share.

同样,如前所述,控制台日志记录工作正常,但从未创建日志文件,因此无法写入。如果有人对这种行为有任何建议或见解,如果他们能分享,我将不胜感激。

EDIT: I'm on Liferay 6.2 b1 and tomcat 7.0.40 (included in Liferay 6.2 bundle)

编辑:我在 Liferay 6.2 b1 和 tomcat 7.0.40(包含在 Liferay 6.2 包中)

回答by KhAn SaAb

I am using log4j.propertiesand its working. try by property file

我正在使用log4j.properties和它的工作。按属性文件尝试

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
log4j.category.SimpleSign=DEBUG,FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
# Set the name of the file
#log4j.appender.FILE.File=${file.name}/something.out
log4j.appender.FILE.File=${file.name}

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true

# Set the DatePattern
log4j.appender.FILE.DatePattern='-' dd-MM-yyyy

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

And set your file path dynamically by calling

并通过调用动态设置您的文件路径

System.setProperty("file.name", FILE PATH);

回答by Gyanendra Dwivedi

Could you please check, if the directory path ="../../logs/portrait-manager ; is a valid one w.r.t the deployment location. Many times, if your directory is not deep enough, (e.g. C:\webapps); this kind of relative path becomes invalid. Try putting a concrete path and concrete file name and see if this is working. Then you could use pattern in the file name and relative file path.

您能否检查一下,如果目录路径 ="../../logs/portrait-manager ; 是一个有效的部署位置。很多时候,如果您的目录不够深,(例如 C:\webapps) ; 这样的相对路径就失效了,尝试输入具体路径和具体文件名,看看是否有效,然后就可以在文件名和相对文件路径中使用pattern了。

Also, the appender definition is missing Threshold parameter. Try adding that and see, if it works.

此外,appender 定义缺少 Threshold 参数。尝试添加它,看看它是否有效。

Edit 1:

编辑1:

A sample config:

示例配置:

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <!-- Dos core appenders -->

    <appender name="serverConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <param name="Threshold" value="DEBUG" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{MM/dd HH:mm:ss} %5p [%-10t] %-10c{1} - %-70m%n" />
        </layout>
    </appender>

        <appender name="serverDebugLogFileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="Threshold" value="DEBUG" />
        <param name="File" value="/apps/logs/dss_debug.log" />
        <param name="MaxFileSize" value="20MB" />
        <param name="MaxBackupIndex" value="40" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{MM/dd HH:mm:ss} %5p [%-10t] %-20c{1} - %-70m%n" />
        </layout>
    </appender>

    <logger name="com.test" additivity="false">
        <level value="DEBUG" />
        <appender-ref ref="serverDebugLogFileAppender" />
        <appender-ref ref="serverConsoleAppender" />
    </logger>

</log4j:configuration>

回答by Vijaya Kumar Govindholla

Add the threshold in the properties file

在属性文件中添加阈值

log4j.appender.default.file.threshold=INFO,DEBUG

In the java file

在java文件中

Ex: Logger APPLICATION_LOGS = Logger.getLogger("rootLogger"); APPLICATION_LOGS.debug("Starting"); or APPLICATION_LOGS.info("Starting"); Works.

例如:Logger APPLICATION_LOGS = Logger.getLogger("rootLogger"); APPLICATION_LOGS.debug("开始"); 或 APPLICATION_LOGS.info("Starting"); 作品。