标准错误日志文件的良好布局是什么?
我正在尝试为我的桌面程序设计错误和警告日志文件。
当我的程序读取用户的输入文件时,它可能会发现语法错误或某种无效数据。一旦所有内容都被读取并且程序正在处理数据,可能会发现更多问题。
我想将有关这些的消息写入一个简单的文本文件。我可能还想包括指示进度、时间、内存使用等的信息文本。我想包括行号,甚至可能是导致错误的实际输入行。
这将是用户想要浏览的文件,因此显然它必须布局良好且易于使用。
您是否知道这方面的任何样式指南,或者您是否见过一个错误日志文件,它让您自己思考:“现在这是一个设计良好的日志文件!”
跟进:
前三个答案实际上更适用于服务器或事件日志。
我真的正在为我的桌面程序寻找一种日志文件格式,以详细说明它在输入文件中发现的任何问题以及其处理的成功(或失败)。
我确信您使用过一些桌面应用程序来生成此类日志文件。你见过什么好的吗?
I am trying to design the error and warning log file for my desktop program.
As my program reads the user's input file, it may find syntax errors or invalid data of some sort. Once everything is read and the program is processing the data, more problems may be found.
I want to write messages about these into a simple text file. I may also want to include informational text to indicate progress, time, memory use, etc. I'll want to include line numbers and maybe even the actual input lines that are causing the errors.
This will be a file that the user will want to browse through, so obviously it has to be well laid out and easy to use.
Do you know of any style guides for this, or have you seen an error log file that has made you think to yourself: "Now that's a well designed log file!"
Followup:
The first three answers are actually more applicable for a server or event log.
I'm really looking for a format for a log file for my desktop program to detail any problems it finds with the input file and the success (or failure) of its processing thereof.
I'm sure there are some desktop applications that you've used that produce such log files. Have you seen any good ones?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
syslog
和log4j
是系统管理员可能熟悉的广泛使用的格式,并且有一些工具可以使用它们。在制作自己的格式之前,您至少应该看看它们。对于 Windows,它几乎总是应用程序事件日志,我并不像文本文件那样喜欢它,但这是正确的方式。我见过一些应用程序在其程序目录或其他内容中写入文本文件,但它始终是临时的,而不是标准的。例如,McAfee VirusScan 将文件记录在 C:\Documents and Settings\All Users\Application Data\McAfee\DesktopProtection 中。我的机器在 C:\Documents and Settings\username\Local Settings\temp*.log 和 C:\WINDOWS\temp*.log 中有大量安装日志。但同样,它们都是不同的、临时的,并且没有明显的精心设计
syslog
andlog4j
are widely used formats with which sysadmins may be familiar, and there are tools for working with them. You should at least look at them before doing your own format.For Windows it's pretty much always the Application Event Log, which I don't really like as much as a text file, but it's the Right Way. I've seen a few apps that write text files in their program directory or something, but it's always ad hoc, never standard. McAfee VirusScan, for example, logs files in C:\Documents and Settings\All Users\Application Data\McAfee\DesktopProtection. My machine has lots of install logs in C:\Documents and Settings\username\Local Settings\temp*.log and C:\WINDOWS\temp*.log. But again, they're all different and ad hoc and not noticeably well-designed
真正让我印象深刻的日志文件很少——实际上,我很难想到任何日志文件(并且未能找到任何日志文件)。根据我的经验,出现问题的部分原因是日志的格式是为了内部人员(程序员)理解,而不是为了另一个程序或外部人员(非程序员)理解。
关键信息经常被遗漏;有时甚至是日期和时间信息。我建议让它们易于解析;我会使用 ISO 8601 表示法,例如“2010-01-22T10:23:21-08:00”(包括时区,请注意)。我会包括进程(和线程)ID;我会考虑包括程序名称、参数(例如文件名);我也会以某种形式包含用户 ID。其中一些可能每次运行只需要一次,但每个消息可能需要其他位。它部分取决于日志文件每次运行是唯一的还是跨运行共享,以及单个文件是否可以由多个用户/进程使用。
然后,您需要决定如何识别消息内容以及消息内容的结尾。特别是对于机器解析(也对于人类解析)来说,如果内容格式明确并且可以检测到结尾,将会很有帮助。您可能需要转义内容(这是最常撤消的步骤,因此如果错误消息是关于格式错误的错误消息,则确实很难分辨文件中的数据以及有关数据的新错误消息在文件中)。在多线程程序中 - 或者日志文件可能在进程之间共享的地方 - 您还必须考虑如何缓冲写入,特别是如果您必须处理长行。您希望日志文件中的每条消息都是单独分开的。这不一定是微不足道的——您甚至可能需要处理锁定协议以确保受控访问。
考虑是否需要为日志提供漂亮的打印机。
考虑基于 XML 的格式(带有开始和结束标记)是否有帮助。我不是 XML 的忠实粉丝,但它确实对机器处理有一些优势。
There are few log files I've been truly impressed by - actually, I struggle to think of any (and am failing to find any). In my experience, the problems that arise come about in part because the logs are formatted for an insider (programmer) to understand, rather than either for another program or an outsider (non-programmer) to understand.
Crucial information is often omitted; sometimes even date and time information. I'd recommend making those easily parsable; I'd use an ISO 8601 notation such as '2010-01-22T10:23:21-08:00' (including the time zone, note). I'd include process (and thread) ID; I'd consider including program name, arguments (eg file name); I'd include the user ID in some form too. Some of this might only be needed once per run, but other bits might be needed for each message. It depends in part on whether the log file is unique per run or shared across runs, and whether a single file can be used by more than one user/process.
You then need to decide how you are going to identify the message content - and the end of the message content. Especially for machine parsing (but also for human parsing), it is helpful if the content is unambiguously formatted and the end can be detected. You may need to escape content (this is the step that is most frequently undone, so if the error message is about a mal-formed error message, it is really hard to tell what's data from the file and what's new error message about the data in the file). In a multi-threaded program - or where the log file might be shared between processes - you have to consider how the writes will be buffered as well, especially if you have to deal with long lines. You want each message to be individually separate in the log file. This is not necessarily trivial--you might even need to deal with a locking protocol to ensure controlled access.
Consider whether you need to provide a pretty-printer for the log.
Consider whether an XML-based format (with begin and end tags) would help. I am not a big fan of XML, but it does have some advantages for machine-processing.
我将日志写入 csv 文件,发现它非常方便(您可以将它们用作数据库或使用电子表格软件打开它们进行复杂分析)。示例日志文件是:
我不确定它是否适合您的需求,但我认为您已经了解了主要想法。祝你好运!
I write logs to csv files and find it very convenient (you can use them as data base or open them with a spreadsheet software for sophisticated analysis). The sample log file is:
I'm not sure whether it suits your needs but I think you've got the main idea. Good luck!