Consider going to a doctor and not being able to communicate your issues. You try to speak but for some reason, you can't. Your lips are making familiar shapes of the words that you are trying to get out but nothing actually gets to the doctor.
考虑去看医生,但无法传达您的问题。 您尝试讲话,但由于某种原因,您无法讲话。 您的嘴唇正在使您想要说出的单词熟悉的形状,但实际上并没有帮助医生。
It is hard to imagine a scenario like that, but this is exactly what is happening with your code. It has a problem and it is trying to speak to you but since you did not care to provide logging, it can't.
很难想象会有这样的情况,但这正是代码所发生的事情。 它有问题,正在尝试与您对话,但是由于您不在乎提供日志记录,因此不能。
In general, there are three ways our code talks to us.
通常,我们的代码通过三种方式与我们对话。
Taking input parameters and responding with the output.
接受输入参数并响应输出。
Gracefully exiting handling cases to maintain consistency of the system.
优雅地退出处理案例以保持系统的一致性。
And crashing, giving us the stack trace or something similar.
崩溃了,给了我们堆栈跟踪或类似的东西。
The default implementation will not give us more information apart from that. To get more information for the above scenarios, we need some type of logging in place. Whenever something important happens we can log the relevant data and analyse those logs when something fails.
默认实现不会为我们提供更多信息。 为了获得有关上述方案的更多信息,我们需要某种类型的日志记录。 每当发生重要事件时,我们可以记录相关数据并在发生故障时分析这些日志。
Not writing logs anywhere is a big issue. Having a good log and monitoring infrastructure becomes a key feature that allows developers to be more prepared to face these problems. And because this is not trivial, from the very beginning this must be a topic that every team should keep in mind.
不在任何地方写日志是一个大问题。 拥有良好的日志和监视基础结构已成为一项关键功能,使开发人员可以为面对这些问题做好准备。 而且由于这并非易事,因此从一开始就必须成为每个团队都应牢记的主题。
Architects must define what, how, and when to log and even how to extract meaningful data from logs. Just like reviewing test coverage, we must keep a sharp eye during reviewing logs in order to ensure that the code satisfies the definitions.
架构师必须定义要记录的内容,方式和时间,甚至如何从日志中提取有意义的数据。 就像检查测试范围一样,在检查日志时,我们必须保持敏锐的眼光,以确保代码满足定义。
Developers and everyone involved in the project must not forget to send logs at least in every layer of the architecture, and testers, for their part, must test that what has been logged is meaningful and provides enough contextual information.
开发人员和参与项目的每个人都不要忘记至少在体系结构的每个层中发送日志,而测试人员则必须测试所记录的内容是否有意义并提供足够的上下文信息。
A logging framework is a utility specifically designed to standardize the process of logging in your application. This can come in the form of a third-party tool, such as log4j. There are several frameworks for logging for different languages. Always look for these aspects before going ahead with any framework:
日志记录框架是专门用于标准化应用程序中的日志记录过程的实用程序。 这可以采用第三方工具的形式,例如log4j 。 有几种用于记录不同语言的框架。 在继续使用任何框架之前,请务必先查找以下方面:
Logging can impact hugely on the performance on your application. Because we have to call the logger multiple times in the codebase, it can become expensive. That is the reason there are several log levels and you should carefully consider which log deserves which severity level.
日志记录可能会对应用程序的性能产生巨大影响。 因为我们必须在代码库中多次调用记录器,所以它可能变得昂贵。 这就是存在多个日志级别的原因,您应该仔细考虑哪个日志应具有哪个严重性级别。
ALL - lowest possible rank and is intended to turn on all logging.DEBUG - fine-grained informational events for debugging.ERROR - error events that might still allow the application to continue running.FATAL - very severe error events that will presumably lead the application to abort.INFO - informational messages that highlight the progress of the application at coarse-grained level.WARN - potentially harmful situations.We can add logs at certain log levels to make sure that we only get the information we need for different environments. Another example that can slow down your application is by using the logger directly like this:
我们可以在某些日志级别添加日志,以确保仅获取不同环境所需的信息。 另一个可能减慢您的应用程序速度的示例是直接使用记录器,如下所示:
logger.debug("Some $expensive message!")This will basically evaluate the expensive message. Which we don't want to do. Instead, do this:
这将基本上评估昂贵的消息。 我们不想这样做。 相反,请执行以下操作:
if (logger.isDebugEnabled) logger.debug("Some $expensive message!")Look for a logging framework with a simple, intuitive API. As a measuring stick, a relatively inexperienced team member should be able to glance at an existing common use or two and understand how to use it himself. Setup should also be easy. And remember logging too much data can be a distracting and poor use of resources.
寻找具有简单直观的API的日志记录框架。 作为量尺,相对缺乏经验的团队成员应该能够浏览一两个现有的常用功能,并了解如何亲自使用它。 设置也应该很容易。 而且请记住,记录过多的数据可能会分散注意力并浪费资源。
It is not enough to log errors in order to use them for troubleshooting purposes. It is also useful to log successful requests so we can have a clear idea of how users work with the application.
记录错误以将其用于故障排除目的是不够的。 记录成功的请求也很有用,这样我们就可以清楚地了解用户如何使用该应用程序。
But this goes beyond that. Logs are also useful to detect common mistakes users make, as well as for security purposes. Writing good logs about a user’s activity can alert us about malicious activity. The end to end user journey can help us figure out the number of users finishing the flow, the number of users dropping out, categories of users etc. This information will have a direct impact on the business as well.
但这还不止于此。 日志对于检测用户犯的常见错误以及出于安全目的也很有用。 编写有关用户活动的良好日志可以使我们警惕恶意活动。 端到端用户之旅可以帮助我们确定完成流程的用户数量,退出的用户数量,用户类别等。此信息也将对业务产生直接影响。
It is important that logs can provide accurate context about what the user was doing when a specific error happened. In this way, having at least one log entry per request/result in every layer of the application is mandatory.
日志可以提供有关特定错误发生时用户正在执行的操作的准确上下文,这一点很重要。 这样,必须在应用程序的每个层中每个请求/结果至少具有一个日志条目。
This works well in a small monolithic application as you have full control over the request and response flows. But what happens when our applications are distributed between various microservices? When a request can travel through the various services before completion?
这在小型整体应用程序中效果很好,因为您可以完全控制请求和响应流。 但是,当我们的应用程序分布在各种微服务之间时,会发生什么呢? 请求何时可以在完成之前遍历各种服务?
Microservice Architecture introduces operational complexity when it comes to monitoring. This is where tracing comes into the picture. A trace is defined as a collection of spans. A span is a single unit that you want to capture. It can be an HTTP request, call to a database, or execution of a message from a queue. It provides visibility into how a request is processed across multiple services in a microservices architecture.
微服务架构在监视方面引入了操作复杂性。 这就是跟踪进入图片的地方。 迹线定义为跨度的集合。 跨度是您要捕获的单个单位。 它可以是HTTP请求,对数据库的调用或队列中消息的执行。 它提供了在微服务架构中跨多个服务如何处理请求的可见性。
This is done using a trace Id for logging which is consistent across microservice calls. This makes it possible to find how a single request travels from one microservice to the other.
这是通过使用跟踪ID进行记录来完成的,该跟踪ID在微服务调用之间是一致的。 这使得可以找到单个请求如何从一个微服务传递到另一个微服务。
Kibana, Zipkin, Jaeger are few of the providers for distributed tracing.
Kibana , Zipkin和Jaeger很少提供分布式跟踪。
Having a good logging infrastructure is important in order to let developers know about the state of every component of the whole system infrastructure. And understanding how to do logging is the next step towards achieving a robust and reliable system.
为了使开发人员了解整个系统基础结构的每个组件的状态,拥有一个好的日志记录基础结构很重要。 并且了解如何进行日志记录是实现强大而可靠的系统的下一步。
Implement logging and allow your services to talk to you!
实现日志记录,并允许您的服务与您交流!
翻译自: https://levelup.gitconnected.com/logs-the-voice-of-code-aca3d159996b