简体   繁体   English

Java Servlet /如何将服务器控制台中写入的日志(log.debug和log.error条目)写入单独的txt文件?

[英]Java Servlet/ How to write the logs(log.debug and log.error entries) written in the server console to a separate txt file?

This is my first java servlet so I would be glad if you can help me and tell me what to do in details :) I can see my program's logs in server console but I need to copy all logs including log.debug and log.error entries to a txt file and later send the txt file via email. 这是我的第一个Java Servlet,因此如果您能帮助我并告诉我详细操作,我将非常高兴:)我可以在服务器控制台中看到程序的日志,但是我需要复制所有日志,包括log.debug和log.error。进入txt文件,然后通过电子邮件发送txt文件。

I have to define the directory/file for the logs. 我必须为日志定义目录/文件。 I have managed this for a regular java program by adding log4j.xml under src folder so I could set the directory in it, it worked, it is writing logs to a txt file created. 我已经通过在src文件夹下添加log4j.xml来为常规的Java程序进行管理,因此我可以在其中设置目录,它可以正常工作,它将日志写入创建的txt文件中。 But I do not know how to do it for the servlet version of my program. 但是我不知道该如何在我的程序的Servlet版本中做到这一点。 How can I set the directory for logs in the servlet and write logs to a specific txt file? 如何设置servlet中日志的目录并将日志写入特定的txt文件?

使用log4j并配置一个FileAppender

You could use log4j too with servlets. 您也可以对servlet使用log4j You just have to add your log4j.xml in the folder YOURWEBAPP/WEB-INF/classes 您只需要在文件夹YOURWEBAPP/WEB-INF/classes添加log4j.xml

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

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