Updated mcollective.init according to OSCI-658
[packages/precise/mcollective.git] / lib / mcollective / logger / syslog_logger.rb
diff --git a/lib/mcollective/logger/syslog_logger.rb b/lib/mcollective/logger/syslog_logger.rb
new file mode 100644 (file)
index 0000000..af9e46a
--- /dev/null
@@ -0,0 +1,51 @@
+module MCollective
+  module Logger
+    # Implements a syslog based logger using the standard ruby syslog class
+    class Syslog_logger<Base
+      require 'syslog'
+
+      include Syslog::Constants
+
+      def start
+        config = Config.instance
+
+        facility = syslog_facility(config.logfacility)
+        level = config.loglevel.to_sym
+
+        Syslog.close if Syslog.opened?
+        Syslog.open(File.basename($0), 3, facility)
+
+        set_level(level)
+      end
+
+      def syslog_facility(facility)
+        begin
+          Syslog.const_get("LOG_#{facility.upcase}")
+        rescue NameError => e
+          STDERR.puts "Invalid syslog facility #{facility} supplied, reverting to USER"
+          Syslog::LOG_USER
+        end
+      end
+
+      def set_logging_level(level)
+        # noop
+      end
+
+      def valid_levels
+        {:info  => :info,
+         :warn  => :warning,
+         :debug => :debug,
+         :fatal => :crit,
+         :error => :err}
+      end
+
+      def log(level, from, msg)
+        Syslog.send(map_level(level), "#{from} #{msg}")
+      rescue
+        # if this fails we probably cant show the user output at all,
+        # STDERR it as last resort
+        STDERR.puts("#{level}: #{msg}")
+      end
+    end
+  end
+end