如何正确处理错误日志?

编程入门 行业动态 更新时间:2024-10-19 08:46:19
本文介绍了如何正确处理错误日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在发布此问题之前,我曾尝试过多次搜索。如果这是重复的,请让我知道,我会删除它。

I tried to do several searches before posting this question. If this is a duplicate, please let me know and I will delete it.

我的问题围绕着正确的方式来处理通过我们的Web应用程序产生的错误。我们目前通过log4j记录一切。如果发生错误,就会在屏幕上显示发生错误,IT部门已被通知,并将尽快纠正。这告诉用户没有任何东西...但是当我们尝试重现错误时,它也不会告诉开发者任何东西。我们必须去错误日志文件夹,并尝试找到这个错误。我还提到这个文件夹是过去一周的日志。每次出现错误时,都会为该用户创建一个日志文件,并将电子邮件发送给被分配处理错误的IT人员。这个电子邮件没有提到日志文件名,但它是在日志文件中写的相同错误文本的副本。

My question revolves around the proper way to handle errors produced through our web application. We currently log everything through log4j. If an error happens, it just says "An error has occurred. The IT Department has been notified and will work to correct this as soon as possible" right on the screen. This tells the user nothing... but it also does not tell the developer anything either when we try to reproduce the error. We have to go to the error log folder and try finding this error. Let me also mention that the folder is full of logs from the past week. Each time there is an error, one log file is created for that user and email is sent to the IT staff assigned to work on errors. This email does not mention the log file name but it is a copy of the same error text written that is in the log file.

所以如果Alicia在7 :15有东西,但还有10个其他错误发生在同一分钟,我必须通过每个日志文件试图找到她。

So if Alicia has a problem at 7:15 with something, but there are 10 other errors that happen that same minute, I have to go through each log file trying to find hers.

我向同事提出的建议是将错误日志表添加到数据库中。这将为每个错误写入记录,记录它是谁,错误,发生了什么页面等。这样做的好处是我们可以从表(error_log_id)返回主键值,显示在具有错误参考ID(1337)的消息的页面上,并已通知正确的IT人员,请保留此参考标识以备将来使用。当我们收到电子邮件时,它会告诉我们错误的ID,以便快速参考。或者如果用户持久,他们可以使用ID与我们联系,我们可以很快找到错误。

What I was proposing to my fellow co-workers is adding an Error Log table into the database. This would write a record to the table for each error, record who it is for, the error, what page it happened on, etc. The bonus of this would be that we can return the primary key value from the table (error_log_id) and show that on the page with a message like "Error Reference Id (1337) has been logged and the proper IT staff has been notified. Please keep this reference id handy for future use". When we get the email, it would tell us the id of the error for quick reference. Or if the user is persistent, they can contact us with the id and we can find the error rather quickly.

如何设置错误日志记录?顺便说一下,我们的系统使用连接到SQL Server数据库的Java Servlet。

How do you setup your error logging? By the way, our system uses Java Servlets that connect to a SQL Server database.

推荐答案

我回答了一个类似的问题这里,但我会适应你的问题。

I answered a similar question here, but I will adapt that answer to your question.

为此,我们使用 requestID - 在处理(过滤器)开始时为每个传入(HTTP)请求分配请求ID,然后在每个日志行上记录,所以你可以很容易地通过该ID来标记这些日志,并找到所有相关的行。

We use requestID for this purpose - assign a request ID to each incoming (HTTP) request, at the very beginning of processing (in filter) and then log that on every log line, so you can easily grep those logs later by that ID and find all relevant lines.

如果你认为将这个ID添加到每个日志语句中是非常繁琐的,那么您并不孤单 - 使用映射诊断环境(MDC)(至少log4j和logback有这个)。

If you think it is very tedious to add that ID to every log statement, then you are not alone - java logging frameworks have made it transparent with the use of Mapped Diagnostic Context (MDC) (at least log4j and logback have this).

RequestID也可以作为一个方便的参考号,吐出来,万一有错误(如你已经建议的那样)。然而,正如其他人已经评论的那样,将这些细节加载到数据库是不明智的 - 更好地使用文件系统。或者,最简单的方法是使用requestID - 那么在发生错误的时候你不需要做任何特殊的事情。它只是帮助您找到正确的日志文件并在该文件内搜索。

RequestID can also work as a handy reference number, to spit out, in case of errors (as you already suggested). However, as others have commented, it is not wise to load those details to database - better use file-system. Or, the simplest approach is to just use the requestID - then you do not need to do anything special at the moment error occurs. It just helps you to locate the correct logfile and search inside that file.

一个请求ID如何?

我们使用以下模式:

< instanceName>:< currentTimeInMillis>。< counter>

<instanceName>:<currentTimeInMillis>.<counter>

由以下变量组成:

  • instanceName 在特定的部署环境中唯一标识特定的JVM /。
  • currentTimeInMillis 是非常不言自明的。我们选择以yyyyMMddHHmmssSSS的人类可读格式表示它,因此很容易从中读取请求开始时间(请注意:SimpleDateFormat不是线程安全的,因此您需要同步或在每个请求中创建一个新的)
  • 计数器是特定毫秒的请求计数器 - 在极少数情况下,您可能需要在一毫秒内生成多个请求ID
  • instanceName uniquely identifies particular JVM in particular deployment environment / .
  • currentTimeInMillis is quite self-explanatory. We chose to represent it in human-readable format "yyyyMMddHHmmssSSS", so it is easy to read request start time from it (beware: SimpleDateFormat is not thread-safe, so you need to either synchronize it or create a new one on each request).
  • counter is request counter in that particular millisecond - in the rare case you might need to generate more than one request ID in one millisecond

正如你所看到的,ID格式已经被设置成这样一种方式,使得 currentTimeInMillis.counter 组合被保证特别是JVM是独一无二的,整个ID保证是全球唯一的(而不是真正意义上的全球),但它对我们的目的而言足够全球化),而不需要涉及数据库或其他中央节点。此外,使用 instanceName 变量可以限制您以后需要查找的日志文件的数量。

As you can see, the ID format has been set up in such a way that currentTimeInMillis.counter combination is guaranteed to be unique in particular JVM and the whole ID is guaranteed to be globally unique (well, not in the true sense of "global", but it is global enough for our purposes), without the need to involve database or some other central node. Also, the use of instanceName variable gives you the possibility to limit the number of log files you later need to look in to find that request.

然后,最终的问题是:在单JVM解决方案中,这是很好的,但是你如何将它扩展到几个JVM,通过某些网络协议进行通信?

Then, the final question: "that is fine and dandy in single-JVM solution, but how do you scale that to several JVMs, communicating over some network protocol?"

当我们使用 Spring Remoting 为了我们的远程目的,我们已经实现了定制 RemoteInvocationFactory (从上下文获取请求ID并将其保存到 RemoteInvocation属性)和 RemoteInvocationExecutor (从属性中获取请求ID,并将其添加到另一个JVM中的诊断上下文)。

As we use Spring Remoting for our remoting purposes, we have implemented custom RemoteInvocationFactory (that takes request ID from context and saves it to RemoteInvocation attributes) and RemoteInvocationExecutor (that takes request ID from attributes and adds it to diagnostic context in the other JVM).

不知道如何用plain -RMI或其他远程处理方法。

Not sure how you would implement it with plain-RMI or other remoting methods.

更多推荐

如何正确处理错误日志?

本文发布于:2023-11-24 04:37:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确处理   错误   日志

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!