简体   繁体   中英

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

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:

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

And my log4j.xml looks something like this:

<?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)

I am using log4j.properties and its working. try by property file

# 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);

Could you please check, if the directory path ="../../logs/portrait-manager ; is a valid one wrt the deployment location. Many times, if your directory is not deep enough, (eg 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.

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

Edit 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>

Add the threshold in the properties file

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

In the java file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM