简单 Logger 实现 C++ 中的详细级别

发布于 2024-12-22 21:26:29 字数 750 浏览 4 评论 0原文

由于我学习 C++,我目前正在为一个项目实现一个简单的记录器。

我已经有了一个 Logger 基类,作为一些不同的附加程序,下一步是实现详细级别。 问题是我不确定我是否正确理解了详细程度的概念,所以我想在开始实施它们之前得到一些反馈。 因此,根据我的理解,详细程度一般如下:

用户首先创建两个记录器: fe:

FileLogger fl; 
VSLogger vl;

之后他可以设置详细级别,如下所示

fl.setLoggerLevel(DEBUG);
vl.setLoggerLevel(FATAL_ERROR);

:之后他可以根据需要进行记录,例如:

fl.logg("New Object of Type .... created");
ASSERT(1,2, "1==2");

当断言以致命错误级别写入 VSLogger 时

,在输出文件中,它可能如下所示:

13:36 Msg: New Object of Type .... created (LEVEL:DEBUG);

在 Visual Studio 中,它可能会看起来像这样:

13:36 Msg: Assert (1==2) failed (LEVEL:FATAL_ERROR)

这是详细级别的含义还是我误解了一般详细级别的概念?

Im currently implementing a simple Logger for a project due my studies in C++.

I already got a Logger base class, as some different appenders and the next step is to implement verbosity levels.
The problem is that I´m not sure if i understood the concept of verbosity levels correctly and so I wanted to get some feebback before I start to implement them.
So from my understanding a verbosity level in general works as following:

User creates two Logger first:
f.e.:

FileLogger fl; 
VSLogger vl;

afterwards he can set the verbosity level like:

fl.setLoggerLevel(DEBUG);
vl.setLoggerLevel(FATAL_ERROR);

afterwards he can logg as he wants, like:

fl.logg("New Object of Type .... created");
ASSERT(1,2, "1==2");

while assert writes into the VSLogger with fatal error level

and in the Ouput file it would probably look like this:

13:36 Msg: New Object of Type .... created (LEVEL:DEBUG);

and in Visual Studio it would probably look like this:

13:36 Msg: Assert (1==2) failed (LEVEL:FATAL_ERROR)

Is that the sence of a verbosity level or have i missunderstood the concepts of verbosity level in general?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷雾森÷林ヴ 2024-12-29 21:26:29

我不明白为什么用户必须使用两个记录器。使用代码不应该关心日志记录目标。

另一个问题是您使用的代码不会在任何地方传递严重性。每当您调用日志记录函数时,您都会传递严重性。使用代码并不关心当前的详细程度是什么,它只是传入一个严重性,并依赖记录器的实现来了解严重性是否超过了详细程度。 (此规则有一个例外,您可以检查详细级别以避免在高性能代码中创建日志消息的开销)

我宁愿有一个记录器作为用户,并带有两个附加输出,这可能有一个不同的详细程度。

在最简单的情况下,我会创建一个全局函数 Logger& GetLogger() 会生成类似 GetLogger().LogDebug("New Object of Type ....created"); 的用户代码

首先创建一个接口:

public class ILogger
{
  public:
    virtual LogDebug(string message)=0;
    ...
}

然后创建一个支持的实例订阅:

public class DispatchingLogger:ILogger
{
  private:
    vector<ILogger*> loggers;
  public:
    override LogDebug(string message)
    {
      foreach(ILogger logger in loggers)
      {
        logger.LogDebug(message);
      }
    }

    void Subscribe(ILogger* logger)
    {
      loggers.add(logger);
    }
}

然后全局 GetLogger() 函数返回 DispatchingLogger 的单个实例。您可以订阅多个具有不同详细级别的实现。实现 ILogger 的用户类也可以注册。

(我知道我的 C++ 语法不正确,自从我使用 C++ 以来已经有一点了)

I don't see why the user should have to work with two loggers. The consuming code should not care about logging targets.

Another issue is that your consuming code doesn't pass in the severity anywhere. Whenever you call a logging function you pass in the severity. The consuming code doesn't care what the current verbosity is, it just passes in a severity and relies on the implementation of the logger to know if the severity exceeds the verbosity level. (There is an exception to this rule, where you check against the verbosity level to avoid the overhead of creating the log message in high performance code)

I'd rather have one logger as the user, with two attached outputs, which might have a different verbosity level.

In the simplest case I'd create a global function Logger& GetLogger() which leads to user code like GetLogger().LogDebug("New Object of Type .... created");

First create an interface:

public class ILogger
{
  public:
    virtual LogDebug(string message)=0;
    ...
}

Then create one instance that supports subscription:

public class DispatchingLogger:ILogger
{
  private:
    vector<ILogger*> loggers;
  public:
    override LogDebug(string message)
    {
      foreach(ILogger logger in loggers)
      {
        logger.LogDebug(message);
      }
    }

    void Subscribe(ILogger* logger)
    {
      loggers.add(logger);
    }
}

Then the global GetLogger() function returns a single instance of DispatchingLogger. And you can subscribe several implementations with different verbosity levels to it. And user classes that implement ILogger can registered too.

(I know my C++ syntax is incorrect, has been a bit since I worked with C++)

风流物 2024-12-29 21:26:29

详细级别显示要记录哪些消息(或者更确切地说,什么关键性)。

例如

Set verbosity to INFO
Log a trace message  //the message will not be logged
Log a debug message  //the message will not be logged
Log an info message  //the message will be logged
Log a warning        //the message will be logged
Log an error or a fatal error.  // will be logged

The verbosity level shows what messages (or rather, of what criticality) are to be logged.

E.g.

Set verbosity to INFO
Log a trace message  //the message will not be logged
Log a debug message  //the message will not be logged
Log an info message  //the message will be logged
Log a warning        //the message will be logged
Log an error or a fatal error.  // will be logged
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文