简体   繁体   English

Log4j2 Amazon ec2 文件日志记录不起作用

[英]Log4j2 Amazon ec2 file logging not working

I just converted my java server to use Log4j2 from 1.我刚刚将我的 Java 服务器从 1 转换为使用 Log4j2。

My file logs worked just fine with Log4j1:我的文件日志与 Log4j1 一起工作得很好:

try {
    Date currentStamp = new Date();
    String sdf = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss").format(currentStamp);
    File theFile = new File("logs/");
    theFile.mkdirs();

    fileAppender = new RollingFileAppender(
            new PatternLayout("[%d{yyyy-MM-dd HH:mm:ss}] %r [%t] %p %C - %m%n"),
            "logs/" + sdf + "_serverLog.txt");
    fileAppender.setMaxFileSize("50MB");
    fileAppender.setMaxBackupIndex(50); //2.5 gigs
} catch (IOException e) {
    e.printStackTrace();
}

Here is my new configuration with Log4j2 (which works fine on my local pc - console/file/email logging works no problem):这是我使用 Log4j2 的新配置(在我的本地 PC 上工作正常 - 控制台/文件/电子邮件日志没有问题):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
        </Console>

        <RollingRandomAccessFile name="FILE" fileName="logs/serverLog.txt" filePattern="logs/serverLog_%d{yyyy-MM-dd}_%i.txt">
            <PatternLayout>
                <Pattern>%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="50"/>
        </RollingRandomAccessFile>

        <SMTP name="EMAIL"
            subject="REMOVED"
            from="REMOVED"
            to="REMOVED"
            smtpHost="REMOVED"
            smtpPort="REMOVED"
            smtpUsername="REMOVED"
            smtpPassword="REMOVED"
            bufferSize="REMOVED">
        </SMTP>
    </Appenders>

    <Loggers>
        <Logger name="com.jayavon.game" level="debug" additivity="false">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="FILE" />
        </Logger>
        <Root level="error" additivity="false">
            <AppenderRef ref="EMAIL" />
        </Root>
    </Loggers>
</Configuration>

My PROD (ec2) server is called via:我的 PROD (ec2) 服务器通过以下方式调用:

nohup /home/ec2-user/bin/startJayServer.sh &

And this script is:这个脚本是:

#!/bin/sh

cd /home/ec2-user/JayServer
CLASSPATH="/home/ec2-user/JayServer/bin:/home/ec2-user/JayServer/lib/*" java -server com.jayavon.game.server.MyServer -PROD > /dev/null 2>logs/server.err

The server startup script and the nohup command were working fine with Log4j1 (ie folder permissions are fine).服务器启动脚本和 nohup 命令与 Log4j1 一起工作正常(即文件夹权限很好)。 On DEV (my windows 10 machine), the console output works in eclipse and files are created (error emails work too).在 DEV(我的 Windows 10 机器)上,控制台输出在 eclipse 中工作并创建文件(错误电子邮件也工作)。 My PROD server starts fine, but no file log is created in my " /home/ec2-user/JayServer/logs " folder.我的 PROD 服务器启动正常,但在我的“ /home/ec2-user/JayServer/logs ”文件夹中没有创建文件日志。 Any ideas?有任何想法吗?

Found my solution on another post " log4j 2 - configuration issue "在另一篇文章“ log4j 2 - 配置问题”上找到了我的解决方案

"I put my log4j2.xml under src/ directory. It works." “我把我的 log4j2.xml 放在 src/ 目录下。它有效。”

Thanks @fstang谢谢@fstang

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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