lib/logsly/outputs.rb in logsly-1.3.1 vs lib/logsly/outputs.rb in logsly-1.3.2

- old
+ new

@@ -2,15 +2,18 @@ require 'logsly/logging182' module Logsly; end module Logsly::Outputs + DEFAULT_PATTERN = '%m\n'.freeze # log the message only + ## NULL class Null - def to_appender(*args); nil; end - def to_layout(*args); nil; end + def data(*args); nil; end + def to_layout(data); nil; end + def to_appender(data); nil; end end ## BASE class Base @@ -19,31 +22,30 @@ def initialize(&build) @build = build || Proc.new{} end - def to_appender(*args) - self.instance_exec(*args, &@build) - self.colors_obj.run_build(*args) - self + def data(*args) + raise NotImplementedError end def to_layout(data) Logsly::Logging182.layouts.pattern(data.to_pattern_opts) end - def to_appender(*args) + def to_appender(data) raise NotImplementedError end end class BaseData def initialize(*args, &build) - @pattern = '%m\n' + @pattern = DEFAULT_PATTERN @colors = nil + @level = nil @args = args self.instance_exec(*@args, &(build || Proc.new{})) end @@ -55,10 +57,15 @@ def colors(value = nil) @colors = value if !value.nil? @colors end + def level(value = nil) + @level = value if !value.nil? + @level + end + def to_pattern_opts Hash.new.tap do |opts| opts[:pattern] = self.pattern if self.pattern if scheme_name = colors_obj.to_scheme(*@args) @@ -77,23 +84,29 @@ ## STDOUT class Stdout < Base - def to_appender(*args) - data = BaseData.new(*args, &self.build) + def data(*args) + BaseData.new(*args, &self.build) + end + + def to_appender(data) Logsly::Logging182.appenders.stdout(:layout => self.to_layout(data)) end end ## FILE class File < Base - def to_appender(*args) - data = FileData.new(*args, &self.build) + def data(*args) + FileData.new(*args, &self.build) + end + + def to_appender(data) Logsly::Logging182.appenders.file(data.path, :layout => self.to_layout(data)) end end @@ -108,14 +121,17 @@ ## SYSLOG class Syslog < Base - def to_appender(*args) + def data(*args) + SyslogData.new(*args, &self.build) + end + + def to_appender(data) ::Syslog.close if ::Syslog.opened? - data = SyslogData.new(*args, &self.build) Logsly::Logging182.appenders.syslog(data.identity, { :logopt => data.log_opts, :facility => data.facility, :layout => self.to_layout(data) }) @@ -124,12 +140,12 @@ end class SyslogData < BaseData def initialize(*args, &build) - super @log_opts = (::Syslog::LOG_PID | ::Syslog::LOG_CONS) @facility = ::Syslog::LOG_LOCAL0 + super end def identity(value = nil) @identity = value if !value.nil? @identity