Class: Twiglet::Logger
- Inherits:
-
Logger
- Object
- Logger
- Twiglet::Logger
- Defined in:
- lib/twiglet/logger.rb
Constant Summary collapse
- DEFAULT_VALIDATION_SCHEMA =
File.read("#{__dir__}/validation_schema.json")
Instance Method Summary collapse
- #configure_validation_error_response(&block) ⇒ Object
- #context_provider(&blk) ⇒ Object
- #debug(message_or_error = nil) ⇒ Object
- #error(message_or_error = nil, error = nil) ⇒ Object
- #info(message_or_error = nil) ⇒ Object
-
#initialize(service_name, **args) ⇒ Logger
constructor
A new instance of Logger.
- #validation_schema(validation_schema) ⇒ Object
- #warn(message_or_error = nil) ⇒ Object (also: #warning)
- #with(default_properties) ⇒ Object
Constructor Details
#initialize(service_name, **args) ⇒ Logger
Returns a new instance of Logger.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/twiglet/logger.rb', line 16 def initialize( service_name, **args ) @service_name = service_name @args = args now = args.fetch(:now, -> { Time.now.utc }) output = args.fetch(:output, $stdout) level = args.fetch(:level, INFO) schema = args.fetch(:validation_schema, DEFAULT_VALIDATION_SCHEMA) raise 'Service name is mandatory' \ unless service_name.is_a?(String) && !service_name.strip.empty? @validator = Validator.new(schema) formatter = Twiglet::Formatter.new( service_name, default_properties: args.fetch(:default_properties, {}), context_providers: Array(args[:context_provider] || args[:context_providers]), now: now, validator: @validator ) super(output, formatter: formatter, level: level) end |
Instance Method Details
#configure_validation_error_response(&block) ⇒ Object
43 44 45 |
# File 'lib/twiglet/logger.rb', line 43 def configure_validation_error_response(&block) @validator.custom_error_handler = block end |
#context_provider(&blk) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/twiglet/logger.rb', line 91 def context_provider(&blk) new_context_providers = Array(@args[:context_providers]) new_context_providers << blk self.class.new( @service_name, **@args, context_providers: new_context_providers ) end |
#debug(message_or_error = nil) ⇒ Object
47 48 49 50 51 |
# File 'lib/twiglet/logger.rb', line 47 def debug( = nil, &) = .is_a?(Exception) ? () : super(, &) end |
#error(message_or_error = nil, error = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/twiglet/logger.rb', line 65 def error( = nil, error = nil, &) = if error (error, ) elsif .is_a?(Exception) () else end super(, &) end |
#info(message_or_error = nil) ⇒ Object
53 54 55 56 57 |
# File 'lib/twiglet/logger.rb', line 53 def info( = nil, &) = .is_a?(Exception) ? () : super(, &) end |
#validation_schema(validation_schema) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/twiglet/logger.rb', line 84 def validation_schema(validation_schema) self.class.new( @service_name, **@args, validation_schema: validation_schema ) end |
#warn(message_or_error = nil) ⇒ Object Also known as: warning
59 60 61 62 63 |
# File 'lib/twiglet/logger.rb', line 59 def warn( = nil, &) = .is_a?(Exception) ? () : super(, &) end |
#with(default_properties) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/twiglet/logger.rb', line 77 def with(default_properties) self.class.new( @service_name, **@args, default_properties: default_properties ) end |