在启动时使用属性来确定要运行的代码段 [英] Use property at startup to determine which section of code to run

查看:196
本文介绍了在启动时使用属性来确定要运行的代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在配置文件中设置了一个调试级别。我们抓住这个价值,将它存储在一个类上的变量中,并从那里使用该值。在服务器启动期间,根据环境的不同,它的打印量大概是500-4000次。

There is a debug level being set in a config file. We grab that value at store it in a variable on a class and use that value from there on out. It is hit roughly 500-4000 times during server startup, depending on the environment.

有一个共同的工作或设计模式,将允许我们在开始时获取该值,然后根据其值运行代码库?最终目标是在if语句的启动过程中不需要从其他类400-4000次查询该参数,以决定是否应该根据所使用的调试级别来记录语句。

Is there a common work around or design pattern that would allow us to get that value at the beginning and then run the code base depending on its value? The end end goal would be to not have to query that parameter from the other class 400-4000 times during startup in an if statement to decide if we are supposed to log a statement or not depending on the debug level being used.

谢谢!

推荐答案

我不知道问题的全部程度,但我会假定它归结为类似于这一点。

I'm not sure to understand the full extent of the problem, but I'll assume it boils down to something similar to this.

我们有一个具有各种日志严重性级别的系统:INFO,WARNING,ERROR

We have a system with various log severity levels: INFO, WARNING, ERROR

和详细层次结构:


  • INFO:INFO,ERROR

  • 调试:INFO,WARNING,ERROR

为了解决这个问题(最基本的形式)可以为严重性定义一个接口:

To solve this problem (in it's most basic form) we may define an interface for severities:

public interface SeverityLoggingStrategy {
    public void log(String msg);
}

记录器:

public class Logger {
    private SeverityLoggingStrategy infoSeverity;
    private SeverityLoggingStrategy warningSeverity;
    private SeverityLoggingStrategy errorSeverity;

    public Logger(SeverityLoggingStrategy infoSeverity, SeverityLoggingStrategy warningSeverity, SeverityLoggingStrategy errorSeverity)         { 
        //... 
    }

    public void info(String msg) {
        this.infoSeverity.log(msg);
    }
    public void warn(String msg) {
        this.warningSeverity.log(msg);
    }
    public void error(String msg) {
       this.errorSeverity.log(msg);
    }
}

然后我们至少需要2个具体的严重性记录策略:

Then we need at least 2 concrete severity logging strategies:

NonLoggingSeverityLoggingStrategy
LoggingSeverityLoggingStrategy

最后一个可以将 SeverityLoggingStrategy s的$ LoggerFactory c> Logger 基于详细级别。

And finally a LoggerFactory that can assemble SeverityLoggingStrategys into a Logger based on a verbosity level.

public class LoggerFactory {
    public forVerbosity(LogVerbosity verbosity) {
        if (verbosity == LogVerbosity.INFO) {
            return new Logger(
                new LoggingSeverityLoggingStrategy(),
                new NonLoggingSeverityLoggingStrategy(),
                new LoggingSeverityLoggingStrategy()
            );
        }

        //...
    }
}

当系统启动时,您可以配置正确的记录器实例使用:

When the system starts you can configure the right instance of the logger to use:

Logger logger = new LoggerFactory().forVerbosity(LogVerbosity.valueOf(config.logVerbosity()));

然后,客户端将使用各种日志严重性级别调用记录器,并且记录器将使用适当的日志记录基于配置的详细程度的严重性策略。

Then clients will be calling upon the logger using various log severity levels and the logger will use the appropriate logging strategy for the severity based on the configured verbosity.

logger.warn('some warning'); //delegates to NonLoggingSeverityLoggingStrategy in INFO verbosity mode

显然,您可能需要比这更多的灵活性,但使用同样的方法,同时扔在几个接口更多的扩展点,你应该很好去。

Obviously you may need more flexibility than this, but using the same approach while throwing in a few interfaces for more extension points you should be good to go.

这篇关于在启动时使用属性来确定要运行的代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆