Class: SimpleStructuredLogger::Writer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/simple_structured_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns a new instance of Writer.



54
55
56
57
58
59
# File 'lib/simple_structured_logger.rb', line 54

def initialize
  @logger = ::Logger.new(STDOUT)
  @default_tags = {}

  set_log_level_from_environment
end

Instance Attribute Details

#default_tagsObject

Returns the value of attribute default_tags.



52
53
54
# File 'lib/simple_structured_logger.rb', line 52

def default_tags
  @default_tags
end

#loggerObject

Returns the value of attribute logger.



52
53
54
# File 'lib/simple_structured_logger.rb', line 52

def logger
  @logger
end

Instance Method Details

#debug(msg, opts = {}) ⇒ Object



91
92
93
# File 'lib/simple_structured_logger.rb', line 91

def debug(msg, opts={})
  @logger.debug("#{msg}: #{stringify_tags(opts)}")
end

#error(msg, opts = {}) ⇒ Object



83
84
85
# File 'lib/simple_structured_logger.rb', line 83

def error(msg, opts={})
  @logger.error("#{msg}: #{stringify_tags(opts)}")
end

#info(msg, opts = {}) ⇒ Object



87
88
89
# File 'lib/simple_structured_logger.rb', line 87

def info(msg, opts={})
  @logger.info("#{msg}: #{stringify_tags(opts)}")
end

#reset_context!Object



69
70
71
# File 'lib/simple_structured_logger.rb', line 69

def reset_context!
  @default_tags = {}
end

#set_context(context) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/simple_structured_logger.rb', line 73

def set_context(context)
  reset_context!

  if SimpleStructuredLogger::Configuration.expand_context
    context = SimpleStructuredLogger::Configuration.expand_context.call(context)
  end

  @default_tags.merge!(context)
end

#set_log_level_from_environmentObject



61
62
63
64
65
66
67
# File 'lib/simple_structured_logger.rb', line 61

def set_log_level_from_environment
  env_log_level = ENV['LOG_LEVEL']

  if !env_log_level.nil? && Logger::Severity.const_defined?(env_log_level.upcase)
    @logger.level = Logger::Severity.const_get(env_log_level.upcase)
  end
end

#warn(msg, opts = {}) ⇒ Object



95
96
97
# File 'lib/simple_structured_logger.rb', line 95

def warn(msg, opts={})
  @logger.warn("#{msg}: #{stringify_tags(opts)}")
end