在java桌面应用程序中使用log4j时如何在特定位置创建日志 [英] how to create log in specific location when using log4j in java desktop application

查看:94
本文介绍了在java桌面应用程序中使用log4j时如何在特定位置创建日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eclipse中使用log4j来记录java桌面应用程序中的消息。我希望日志应该在特定的文件夹中创建(具体来说,在包含源文件夹'src'和类文件夹'bin'的文件夹中)。

I am using log4j in Eclipse for logging messages in a java desktop application. I want that the log should be created in a specific folder (Specifically, in the folder which contains source folder 'src' and classes folder 'bin').

是吗可以在log4j.properties中设置吗?如何确保仅在此位置创建日志?

Is it possible to set this in log4j.properties? How to ensure that log is created at this location only?

推荐答案

我会使用Saket的回复。但是,不是硬编码位置,它总是更好地拥有相对路径。

I would go with Saket's reply. But instead of hardcoding the location its always better to have a relative path.

例如,如果您从一个名为Launcher的类的main方法启动应用程序,那么这就是Eclipse Project目录的结构:

If you started your application from a main method from a class called Launcher for example and this is the structure of your Eclipse Project directory:

Java Project
   src
   bin

然后只需将您的位置设为

Then just give your location to be

 log4j.appender.R.File=./log/Logfile.log

这将在目录日志下创建文件:

This will create the file under a directory log:

Java Project
   src
   bin
   log
     LogFile.log

希望你得到它..
:)

Hope you got it.. :)

这篇关于在java桌面应用程序中使用log4j时如何在特定位置创建日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆