Class: Semlogger

Inherits:
Logger show all
Defined in:
lib/semlogger.rb,
lib/semlogger.rb

Direct Known Subclasses

Default

Defined Under Namespace

Classes: Base, CustomType, Default, Filter, Formatter, Multiplex, Output, Printer, Rack, Rotate, Tailer, Writer

Constant Summary collapse

@@progname =
nil

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logdev = nil, *a, &e) ⇒ Semlogger

Returns a new instance of Semlogger.



100
101
102
103
104
105
106
107
# File 'lib/semlogger.rb', line 100

def initialize logdev = nil, *a, &e
	case logdev
	when String, nil then logdev = ::Semlogger::Writer.new logdev
	end
	@progname = a[0] || @@progname
	@level, @data, @tags, @logdev = DEBUG, {}, [], logdev
	self.class.logger = self  if !self.class.logger && self.class.logger.is_a?( Semlogger::Default)
end

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



73
74
75
# File 'lib/semlogger.rb', line 73

def logger
  @logger
end

.prognameObject

Returns the value of attribute progname.



73
74
75
# File 'lib/semlogger.rb', line 73

def progname
  @progname
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



66
67
68
# File 'lib/semlogger.rb', line 66

def level
  @level
end

#logdevObject

Returns the value of attribute logdev.



66
67
68
# File 'lib/semlogger.rb', line 66

def logdev
  @logdev
end

#prognameObject

Returns the value of attribute progname.



66
67
68
# File 'lib/semlogger.rb', line 66

def progname
  @progname
end

Class Method Details

.custom(*a) ⇒ Object



94
# File 'lib/semlogger.rb', line 94

def custom( *a)  CustomType.new( *a).tap {|t| t.logger = self.logger }  end

.new_rails_logger(config, logdev = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/semlogger.rb', line 75

def new_rails_logger config, logdev = nil
	require 'semlogger/rack'
	logdev ||= ::Rails.root.join 'log', "#{::Rails.env.to_s.gsub('%', '%%')}.%Y-%m-%d.%$.log"
	logdev = logdev.to_s
	logger = nil
	if Rails.env.production?
		logger = new logdev
		logger.level = Semlogger::INFO
	elsif Rails.env.development?
		logger = new Semlogger::Multiplex.new( Semlogger::FInfo.new( Semlogger::Printer.new), Semlogger::Writer.new( logdev))
		logger.level = Semlogger::DEBUG
	else
		logger = new logdev
		logger.level = Semlogger::DEBUG
	end
	config.middleware.swap Rails::Rack::Logger, Semlogger::Rack, [], {reqid: :uuid}
	config.logger = logger
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/semlogger.rb', line 118

def add severity, message = nil, progname = nil, &block
	severity ||= UNKNOWN
	return true  if @logdev.nil? or severity < @level
	progname ||= @progname
	if message.nil?
		if block_given?
			message = yield
		else
			message = progname
			progname = @progname
		end
	end
	@logdev.add severity, Time.new, progname, format_data( @data), format_tags( @tags), format_msg( message)
end

#caller(&e) ⇒ Object



167
168
169
# File 'lib/semlogger.rb', line 167

def caller &e
	data caller: Kernel.method(:caller), &e
end

#custom(*a) ⇒ Object



96
# File 'lib/semlogger.rb', line 96

def custom( *a)  CustomType.new( *a).tap {|t| t.logger = self }  end

#data(data, &e) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/semlogger.rb', line 158

def data data, &e
	return @data  unless e
	@data.update data
	keys = data.keys
	yield
ensure
	keys.each &data.method( :delete)
end

#format_data(data) ⇒ Object



153
154
155
156
# File 'lib/semlogger.rb', line 153

def format_data data
	data = data ? data.dup : {}
	data.each {|k,v| data[k] = format_obj v }
end

#format_msg(msg) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/semlogger.rb', line 133

def format_msg msg
	msg = msg.to_semlogger
	case msg
	when Array then msg
	else [msg.class.name.to_sym, msg.inspect]
	end
end

#format_obj(obj) ⇒ Object



141
142
143
144
145
146
# File 'lib/semlogger.rb', line 141

def format_obj obj
	case obj
	when Proc then obj.call
	else obj
	end
end

#format_tags(tags) ⇒ Object



148
149
150
151
# File 'lib/semlogger.rb', line 148

def format_tags tags
	tags = tags ? tags.dup : []
	tags.map &method( :format_obj)
end

#log_levelObject



70
# File 'lib/semlogger.rb', line 70

def log_level()  @level  end

#log_level=(level) ⇒ Object

some libs use #log_level



69
# File 'lib/semlogger.rb', line 69

def log_level=( level)  @level = level  end

#tagged(*tags, &e) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/semlogger.rb', line 109

def tagged *tags, &e
	@tags += tags.flatten.compact
	tags = tags.size
	yield
ensure
	#tags.times { @tags.pop }
	@tags.slice! -tags .. -1
end

#thread(&e) ⇒ Object



171
172
173
# File 'lib/semlogger.rb', line 171

def thread &e
	data thread: Proc.new { Thread.current.object_id }, &e
end