为什么我们要把«Logger»类看作是一个单子?[英] Why we should consider the «Logger» class as a singleton?

本文是小编为大家收集整理的关于为什么我们要把«Logger»类看作是一个单子?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我们都知道日志,好的,但是为什么我们应该考虑" logger"一类单身人士呢?如果我们将其作为普通 non -singleton类做出什么?

推荐答案

我在IBM网站上找到了这一点.它很好地解释了Logger Singleton课程的用法.

真正单身人士的经典示例 是伐木服务.假设我们有 基于事件的伐木服务:客户 对象请求文本记录 向记录发送消息 服务.其他对象实际日志 文字某个地方(控制台,文件, 无论如何)通过收听记录 这些记录请求的服务 处理它们.首先,请注意 伐木服务通过了经典 测试是单身人士:

  • 请求者需要一个将请求发送到的对象 日志.这意味着全球 访问.
  • 由于记录服务是单个事件源 听众可以注册,只有 需要是一个实例.

在这里链接:明智地使用您的Singletons P>

如果您不使用Singleton类,则必须在这些不同的Logger实例之间处理同步(写入文件或您使用的任何流).因此,当您只有一个全局记录器实例时,它要容易得多.

其他推荐答案

主要问题是实际日志的持续存在.

如果您在文件系统上编写一个以上的实例(因此,可能多个线程)可能会导致乱码文件.

从某种意义上说,取决于缓冲和其他低级机制消息可能最终与其他人的消息(或消息的一部分)混合在一起.

这可能是一个小问题,但这是我唯一可以想到的关于只有一个(串行)日志写作对象的问题.

其他推荐答案

如果您有一个以上具有不同内容的日志流,则可以使用针对不同输出初始化的Logger类的多个实例.

但是,如果您只有一个日志流,则具有多个Logger类实例会导致更复杂的实现,因为这些实例必须共同努力以管理实际资源.例如,考虑一个记录每个消息的记录器.两个实例将不得不同步其序列计数器,这要求他们彼此相处,协商计数器的增加等等. (在静态类成员中共享计数器的替代方案等同于拥有单例记录器)

本文地址:https://www.itbaoku.cn/post/627793.html

问题描述

We all know about log, ok, but why should we consider the «Logger» class a singleton one? What happens if we make it as a normal non-singleton class?

推荐答案

I found this here on the IBM site. It explains the usage of a Logger Singleton class quite well.

A classic example of a true singleton is a logging service. Suppose we have an event-based logging service: Client objects request that text be logged by sending a message to the logging service. Other objects actually log the text somewhere (console, file, whatever) by listening to the logging service for these logging requests and handling them. First, notice that the logging service passes the classic test for being a singleton:

  • The requesters need a well-known object to which to send requests to log. This means a global point of access.
  • Since the logging service is a single event source to which multiple listeners can register, there only needs to be one instance.

Here the link: Use your singletons wisely

If you wouldn't use a singleton class you would have to deal with the synchronisation (writing to a file, or whatever stream you use) between these different logger instances. So its much easier, when you just have one global Logger instance.

其他推荐答案

The main problem is where the actual log is persisted.

If you are writing on a filesystem, having more than one instance (and therefore, probably, more than one thread) may result in a garbled file.

In the sense that depending on buffering and other low-level mechanisms messages from one write may end up mixed with messages (or parts of messages) from others.

This may be a minor problem, but it's the only one I can think of regarding having just one (and therefore serial) log writing object.

其他推荐答案

If you have more than one log streams with different content, you can use multiple instances of the logger class initialized for the different outputs.

However, if you have only one log stream, having multiple logger class instances leads to more complex implementation, as the instances have to work together to manage the actual resource. Consider for example a logger that logs each message with a sequence number. Two instances will have to synchronize their sequence counters, which requires them to knwp about each other, negotiate counter increases and so on. (The alternative of having shared counter in a static class member is equivalent to having a singleton logger)