如何将时间戳附加到 java.util.logging.FileHandler.pattern 的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2505721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:32:04  来源:igfitidea点击:

how to append timestamp to file name for java.util.logging.FileHandler.pattern

javatimestampfilenamesjava.util.logging

提问by Alex Le

Hi I was wondering if anyone knows a way to append a timestamp to the log file name specified through logging.properties java.util.logging.FileHandler.pattern

嗨,我想知道是否有人知道将时间戳附加到通过 logging.properties java.util.logging.FileHandler.pattern 指定的日志文件名的方法

seems like something pretty straight forward but I cant seem to find a solution to this anywhere.

似乎很简单,但我似乎无法在任何地方找到解决方案。

Thanks

谢谢

回答by Matthieu BROUILLARD

I am afraid that just by configuration you can't set the file name in teh way you want.

恐怕仅通过配置您无法以您想要的方式设置文件名。

Have a look at the code in FileHandler.generate()to convince you.

看看里面的代码FileHandler.generate()说服你。

What you can do is write your own FileHandler that will handle this naming or switch to another log framework.

您可以做的是编写自己的 FileHandler 来处理此命名或切换到另一个日志框架。

If you use java.util.logging, I wrote some years ago a Formatter & a Handlerthat can still be usefull, feel free to use.

如果您使用 java.util.logging,我几年前写了一个Formatter & 一个 Handler仍然有用,请随意使用。

回答by jay

You could instantiate the FileHandlerin code with pattern, limit, count etc parameters. So, the pattern string can be formed consisting of date and time.

您可以FileHandler使用模式、限制、计数等参数实例化in 代码。因此,可以形成由日期和时间组成的模式字符串。

Example Code:

示例代码:

String timeStamp = new SimpleDateFormat().format( new Date() );
FileHandler fh = new FileHandler( "./jay_log_%u.%g_" + timeStamp + ".log", 30000, 4);
logger.addHandler(fh);

回答by cruxion effux

To create a file named the current date/time:

创建一个名为当前日期/时间的文件:

Date date = new Date() ; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ; File file = new File(dateFormat.format(date) + ".txt") ;

Date date = new Date() ; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ; File file = new File(dateFormat.format(date) + ".txt") ;

回答by Kannan Ekanath

You could use SLF4J which points again back to java.util.logging packages which seems to have this feature http://javablog.co.uk/2008/07/12/logging-with-javautillogging/

您可以使用 SLF4J,它再次指向 java.util.logging 包,它似乎具有此功能 http://javablog.co.uk/2008/07/12/logging-with-javautillogging/

alternatively for a no-third-party-frameworks approach you can use a CustomFormatter a sample of which is already available here, http://www.kodejava.org/examples/458.html

或者,对于无第三方框架的方法,您可以使用 CustomFormatter,其示例已在此处提供, http://www.kodejava.org/examples/458.html