Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 logs into user home folder

Tags:

java

log4j

log4j2

I'm trying to save log4j2 logs into user home folder because the user has not right to write where the application is installed.

This is the relevant part of my log4j settings. Unfortunately it doesn't resolve user.home and so it creates the folder $user.home where the application is running.

    <?xml version="1.0" encoding="UTF-8"?>
<configuration name="defaultConfiguration" status="warn" strict="true" monitorInterval="270">
    <properties>        
        <property name="patternlayout">%d{dd/MM/yyyy HH:mm:ss} %5p %c{1}:%L - %m%n
        </property>
        <property name="filename">${user.home}/niro/logs/niroDesk.log</property>
        <property name="filenamePattern">${user.home}/niro/logs/niroDesk.log-%d{yyyy-MM-dd}.log.gz
        </property>
    </properties>

    <appenders>
        <appender name="Console" type="Console" target="SYSTEM_OUT">
            <layout type="PatternLayout" pattern="${patternlayout}" />
        </appender>

        <!-- Log su file ogni giorno, e mantenimento degli ultimi 5 files -->
        <RollingFile name="File" fileName="${filename}" filePattern="${filenamePattern}" bufferedIO="true" immediateFlush="true" append="true">
            <PatternLayout pattern="${patternlayout}" />
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
            <DefaultRolloverStrategy>
                <Delete basePath="${user.home}/niro/logs" maxDepth="2">
                    <IfFileName glob="niroDesk*.log.gz" />
                    <IfLastModified age="7d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

        <appender name="AsyncFile" type="async" blocking="true" bufferSize="128">
            <appender-ref ref="File" />
        </appender>
    </appenders>
    <loggers>
        <root level="error">
            <appender-ref ref="Console" />
            <appender-ref ref="AsyncFile" />
        </root>
        <logger level="warn" name="org.springframework" additivity="false">
            <appender-ref ref="Console" />
            <appender-ref ref="AsyncFile" />
        </logger>   
    </loggers>
</configuration>


%d{dd/MM/yyyy HH:mm:ss} %5p %c{1}:%L - %m%n ${user.home}/niro/logs/niroDesk.log ${user.home}/niro/logs/niroDesk.log-%d{yyyy-MM-dd}.log.gz

<appenders>
    <appender name="Console" type="Console" target="SYSTEM_OUT">
        <layout type="PatternLayout" pattern="${patternlayout}" />
    </appender>

    <!-- Log su file ogni giorno, e mantenimento degli ultimi 5 files -->
    <RollingFile name="File" fileName="${filename}" filePattern="${filenamePattern}" bufferedIO="true" immediateFlush="true" append="true">
        <PatternLayout pattern="${patternlayout}" />
        <Policies>
            <TimeBasedTriggeringPolicy />
        </Policies>
        <DefaultRolloverStrategy>
            <Delete basePath="${user.home}/niro/logs" maxDepth="2">
                <IfFileName glob="niroDesk*.log.gz" />
                <IfLastModified age="7d" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>

    <appender name="AsyncFile" type="async" blocking="true" bufferSize="128">
        <appender-ref ref="File" />
    </appender>
</appenders>
<loggers>
    <root level="error">
        <appender-ref ref="Console" />
        <appender-ref ref="AsyncFile" />
    </root>
    <logger level="warn" name="org.springframework" additivity="false">
        <appender-ref ref="Console" />
        <appender-ref ref="AsyncFile" />
    </logger>   
</loggers>

There is a way to solve this issue?

like image 801
drenda Avatar asked Nov 12 '16 17:11

drenda


1 Answers

I solved the issue. You sould use ${sys:user.home} instead of ${user.home}.

like image 170
drenda Avatar answered Oct 05 '22 23:10

drenda