Module: RazorRisk::Cassini::Diagnostics

Includes:
RazorRisk::Core::Diagnostics::Logger
Defined in:
lib/razor_risk/cassini/diagnostics/util_functions.rb

Defined Under Namespace

Modules: SetupDiagnosticLogging_Constants

Instance Method Summary collapse

Instance Method Details

#setup_diagnostic_logging(program_name, log_directory, log_threshold, **options) ⇒ Object

configure diagnostic logging:

  • set program name
  • set services:
    • benchmark logger : file, only responding to :benchmark, and only if --benchmark is specified
    • main file logger : file, stores all except :benchmark, according to :log_threshold
    • main cons logger : stdout/stderr, only :notice and above

Signature

  • Parameters:

    - program_name

    (::String) The program name. May not be nil

    - log_directory

    (::String) The directory in which log-files will be created. May not be nil, unless both :no_benchmark_log and :no_main_log are truey

    - log_threshold

    (::String | ::Symbol |[ ::Symbol, ::Symbol ] ) Specifies the log-threshold(s). The threshold(s) is specified as a string containing comma-separated values, or as a single symbol, or as an array of symbols, all of which are treated as a sequence of severity thresholds. The severity thresholds apply, respectively, to the modifiable-sequence of Console and Main.

  • Options:

    • :no_benchmark_log:: (boolean) Unless truey, a benchmark log will be created
    • :no_console_log:: (boolean) Unless truey, a console log will be created
    • :no_main_log:: (boolean) Unless truey, a main log will be created

NOTE: If all of :no_benchmark_log, :no_console_log, and :no_main_log are truey, a null logger will be created, and there will be no logging



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/razor_risk/cassini/diagnostics/util_functions.rb', line 88

def setup_diagnostic_logging program_name, log_directory, log_threshold, **options

    ::Xqsr3::Quality::ParameterChecking.check_parameter program_name, 'program_name', type: ::String
    ::Xqsr3::Quality::ParameterChecking.check_parameter log_directory, 'log_directory', type: ::String
    ::Xqsr3::Quality::ParameterChecking.check_parameter log_threshold, 'log_threshold', types: [ ::Array, ::Integer, ::Symbol ]

    case log_threshold
    when ::Array

        log_thresholds  =   log_threshold.dup
    when ::Symbol

        log_thresholds  =   [ log_threshold ]
    else

        log_thresholds  =   []
    end

    console_threshold   =   log_thresholds.shift || options[:console_threshold] || SetupDiagnosticLogging_Constants::DEFAULT_LOG_THRESHOLD_CONSOLE
    main_threshold      =   log_thresholds.shift || options[:main_threshold] || SetupDiagnosticLogging_Constants::DEFAULT_LOG_THRESHOLD_MAIN

    console_threshold_v =   ::Integer === console_threshold ? console_threshold : SetupDiagnosticLogging_Constants::STOCK_SEVERITY_LEVEL_VALUES[console_threshold]
    main_threshold_v    =   ::Integer === main_threshold ? main_threshold : SetupDiagnosticLogging_Constants::STOCK_SEVERITY_LEVEL_VALUES[main_threshold]

    unless options[:no_benchmark_log] && options[:no_main_log]

        unless File.directory?(log_directory)

            climate.abort "log directory '#{log_directory}' exists and is not a directory" if File.exist?(log_directory)

            begin

                FileUtils.mkdir_p log_directory
            rescue => x

                log :alert, "exception(#{x.class}): #{x.message}"

                climate.abort x.message
            end
        end
    end

    services = []

    unless options[:no_benchmark_log]

        bm_log_file_name    =   program_name + '-benchmark.log'

        bm_log_file_path    =   File.join(log_directory, bm_log_file_name)

        bm_logger           =   Pantheios::Services::SimpleFileLogService.new bm_log_file_path, roll_size: SetupDiagnosticLogging_Constants::DEFAULT_LOG_SIZE_BENCHMARK, roll_depth: SetupDiagnosticLogging_Constants::DEFAULT_LOG_DEPTH

        def bm_logger.severity_logged? severity

            case severity
            when :benchmark, SetupDiagnosticLogging_Constants::BENCHMARK_SEVERITY_VALUE

                true
            else

                false
            end
        end

        services            <<  bm_logger
    end

    unless options[:no_main_log]

        mf_log_file_name    =   program_name + '-main.log'

        mf_log_file_path    =   File.join(log_directory, mf_log_file_name)

        mf_logger           =   Pantheios::Services::SimpleFileLogService.new mf_log_file_path, roll_size: SetupDiagnosticLogging_Constants::DEFAULT_LOG_SIZE_MAIN, roll_depth: SetupDiagnosticLogging_Constants::DEFAULT_LOG_DEPTH

        mf_logger.instance_variable_set :@main_threshold_v_, main_threshold_v

        def mf_logger.severity_logged? severity

            return true unless @main_threshold_v_

            severity = :debug4 if :debug == severity

            severity_v = nil

            case severity
            when :benchmark, SetupDiagnosticLogging_Constants::BENCHMARK_SEVERITY_VALUE

                return false
            when ::Symbol

                severity_v = SetupDiagnosticLogging_Constants::STOCK_SEVERITY_LEVEL_VALUES[severity]
            when ::Integer

                severity_v = severity
            else

                ;
            end

            return true unless severity_v

            severity_v <= @main_threshold_v_
        end

        services            <<  mf_logger
    end

    unless options[:no_console_log]

        mc_logger           =   Pantheios::Services::SimpleConsoleLogService.new

        mc_logger.instance_variable_set :@console_threshold_v_, console_threshold_v

        def mc_logger.severity_logged? severity

            return true unless @console_threshold_v_

            severity = :debug4 if :debug == severity

            severity_v = nil

            case severity
            when :benchmark, SetupDiagnosticLogging_Constants::BENCHMARK_SEVERITY_VALUE

                return false
            when ::Symbol

                severity_v = SetupDiagnosticLogging_Constants::STOCK_SEVERITY_LEVEL_VALUES[severity]
            when ::Integer

                severity_v = severity
            else

                ;
            end

            return true unless severity_v

            severity_v <= @console_threshold_v_
        end

        services            << mc_logger
    end

    if services.empty?

        mx_logger   =   Pantheios::Services::NullLogService.new
    else

        mx_logger   =   Pantheios::Services::MultiplexingLogService.new services, level_cache_mode: :thread_fixed
    end

    ::Pantheios::Core.set_service mx_logger
end