├── .document ├── Changelog.md ├── Gemfile ├── test ├── helper.rb ├── tcp_syslog_benchmark.rb └── test_tcp_syslog.rb ├── .gitignore ├── LICENSE.txt ├── README.rdoc ├── Rakefile ├── tcp_syslog.gemspec └── lib └── tcp_syslog.rb /.document: -------------------------------------------------------------------------------- 1 | lib/**/*.rb 2 | bin/* 3 | - 4 | features/**/*.feature 5 | LICENSE.txt 6 | -------------------------------------------------------------------------------- /Changelog.md: -------------------------------------------------------------------------------- 1 | ## 1.0.3 (2011-11-26) 2 | 3 | * Added SSL support with peer verification 4 | 5 | ## 1.0.2 (2011-06-29) 6 | 7 | * Relax version requirements for active_support (Yawn) 8 | * Remove Gemfile.lock (Yawn) 9 | 10 | ## 1.0.1 (2011-06-17) 11 | 12 | * Fix runtime dep 13 | 14 | ## 1.0.0 (2011-06-17) 15 | 16 | * Initial version 17 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | # Add dependencies required to use your gem here. 3 | # Example: 4 | # gem "activesupport", ">= 2.3.5" 5 | 6 | # Add dependencies to develop your gem here. 7 | # Include everything needed to run rake, tests, features, etc. 8 | group :development do 9 | gem "shoulda", ">= 0" 10 | gem "yard", "~> 0.6.0" 11 | gem "bundler", "~> 1.0.0" 12 | gem "jeweler", "~> 1.5.1" 13 | gem "rcov", ">= 0" 14 | gem 'activesupport', '~> 3.0.0' 15 | gem "rr" 16 | gem 'i18n' 17 | gem 'SystemTimer' 18 | end 19 | -------------------------------------------------------------------------------- /test/helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler' 3 | begin 4 | Bundler.setup(:default, :development) 5 | rescue Bundler::BundlerError => e 6 | $stderr.puts e.message 7 | $stderr.puts "Run `bundle install` to install missing gems" 8 | exit e.status_code 9 | end 10 | require 'test/unit' 11 | require 'shoulda' 12 | require 'rr' 13 | require 'active_support' 14 | require 'timeout' 15 | 16 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 17 | $LOAD_PATH.unshift(File.dirname(__FILE__)) 18 | require 'tcp_syslog' 19 | 20 | class Test::Unit::TestCase 21 | include RR::Adapters::TestUnit 22 | end 23 | -------------------------------------------------------------------------------- /test/tcp_syslog_benchmark.rb: -------------------------------------------------------------------------------- 1 | require 'helper' 2 | require 'benchmark' 3 | 4 | logger_tcp = TcpSyslog.new 5 | logger_file = ActiveSupport::BufferedLogger.new("/tmp/logfile") 6 | 7 | Benchmark.bm do |b| 8 | b.report("File (100 times)") do 9 | 100.times { logger_file.info("benchmark") } 10 | end 11 | 12 | b.report("TCP (100 times)") do 13 | 100.times { logger_tcp.info("benchmark") } 14 | end 15 | 16 | logger_file.auto_flushing = 20 17 | b.report("File (100 times, buffer = 20)") do 18 | 100.times { logger_file.info("benchmark") } 19 | end 20 | 21 | logger_tcp.auto_flushing = 20 22 | b.report("TCP (100 times, buffer = 20)") do 23 | 100.times { logger_tcp.info("benchmark") } 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # rcov generated 2 | coverage 3 | 4 | # rdoc generated 5 | rdoc 6 | 7 | # yard generated 8 | doc 9 | .yardoc 10 | 11 | # bundler 12 | .bundle 13 | 14 | # jeweler generated 15 | pkg 16 | 17 | # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore: 18 | # 19 | # * Create a file at ~/.gitignore 20 | # * Include files you want ignored 21 | # * Run: git config --global core.excludesfile ~/.gitignore 22 | # 23 | # After doing this, these files will be ignored in all your git projects, 24 | # saving you from having to 'pollute' every project you touch with them 25 | # 26 | # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line) 27 | # 28 | # For MacOS: 29 | # 30 | #.DS_Store 31 | # 32 | # For TextMate 33 | #*.tmproj 34 | #tmtags 35 | # 36 | # For emacs: 37 | #*~ 38 | #\#* 39 | #.\#* 40 | # 41 | # For vim: 42 | #*.swp 43 | 44 | Gemfile.lock 45 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Tech-Angels SAS 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = tcp_syslog 2 | 3 | Rails TCP syslog logger. 4 | This logger sends logs to syslog using TCP instead of UDP. 5 | This is much less efficient than logging to file, but required sometimes in large architectures. 6 | 7 | == Usage 8 | 9 | === Installation 10 | 11 | Use bundle to install : 12 | 13 | gem 'tcp_syslog' 14 | 15 | === Configuration 16 | 17 | Add this line to your rails configuration : 18 | 19 | config.logger = TcpSyslog.new(:progname => 'your_rails_app_name') 20 | 21 | Options are detailled in doc. 22 | 23 | == Contributing to tcp_syslog 24 | 25 | * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet 26 | * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it 27 | * Fork the project 28 | * Start a feature/bugfix branch 29 | * Commit and push until you are happy with your contribution 30 | * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. 31 | * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it. 32 | 33 | == Copyright 34 | 35 | Copyright (c) 2011 Tech-Angels SAS. See LICENSE.txt for 36 | further details. 37 | 38 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler' 3 | begin 4 | Bundler.setup(:default, :development) 5 | rescue Bundler::BundlerError => e 6 | $stderr.puts e.message 7 | $stderr.puts "Run `bundle install` to install missing gems" 8 | exit e.status_code 9 | end 10 | require 'rake' 11 | 12 | require 'jeweler' 13 | Jeweler::Tasks.new do |gem| 14 | # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options 15 | gem.name = "tcp_syslog" 16 | gem.homepage = "http://github.com/tech-angels/tcp_syslog" 17 | gem.license = "MIT" 18 | gem.summary = %Q{Send logs to syslog using TCP instead of UDP} 19 | gem.description = %Q{Rails syslog logger using TCP instead of UDP} 20 | gem.email = "philippe.lafoucriere@gmail.com" 21 | gem.authors = ["Philippe Lafoucrière"] 22 | gem.version = '1.0.3' 23 | # Include your dependencies below. Runtime dependencies are required when using your gem, 24 | # and development dependencies are only needed for development (ie running rake tasks, tests, etc) 25 | # gem.add_runtime_dependency 'jabber4r', '> 0.1' 26 | gem.add_runtime_dependency 'activesupport', '~> 3.0.0' 27 | gem.add_runtime_dependency 'SystemTimer', '>=1.2.3' 28 | # gem.add_development_dependency 'rspec', '> 1.2.3' 29 | end 30 | Jeweler::RubygemsDotOrgTasks.new 31 | 32 | require 'rake/testtask' 33 | Rake::TestTask.new(:test) do |test| 34 | test.libs << 'lib' << 'test' 35 | test.pattern = 'test/**/test_*.rb' 36 | test.verbose = true 37 | end 38 | 39 | require 'rcov/rcovtask' 40 | Rcov::RcovTask.new do |test| 41 | test.libs << 'test' 42 | test.pattern = 'test/**/test_*.rb' 43 | test.verbose = true 44 | end 45 | 46 | task :default => :test 47 | 48 | Rake::TestTask.new(:benchmark) do |bm| 49 | bm.libs << 'lib' << 'test' 50 | bm.pattern = 'test/**/*_benchmark.rb' 51 | bm.verbose = true 52 | end 53 | require 'yard' 54 | YARD::Rake::YardocTask.new 55 | -------------------------------------------------------------------------------- /test/test_tcp_syslog.rb: -------------------------------------------------------------------------------- 1 | require 'helper' 2 | 3 | class TestTcpSyslog < ActiveSupport::TestCase 4 | 5 | context "A TcpSyslogger" do 6 | setup do 7 | @logger = TcpSyslog.new 8 | end 9 | 10 | should "have set defaults correctly" do 11 | assert_equal 'localhost', @logger.host 12 | assert_equal '514', @logger.port 13 | assert_equal Syslog::LOG_USER, @logger.facility 14 | assert_equal 'rails', @logger.progname 15 | assert_equal 1, @logger.auto_flushing 16 | assert_equal Logger::DEBUG, @logger.level 17 | assert_equal false, @logger.ssl? 18 | end 19 | 20 | should "have defined debug, info, warn, error, fatal methods" do 21 | assert @logger.respond_to?(:debug) 22 | assert @logger.respond_to?(:info) 23 | assert @logger.respond_to?(:warn) 24 | assert @logger.respond_to?(:error) 25 | assert @logger.respond_to?(:fatal) 26 | end 27 | 28 | context "on add message" do 29 | setup do 30 | @message = "This message to be sent to syslog" 31 | end 32 | 33 | should "have sent the message to syslog using plain TCP" do 34 | mock.proxy(TCPSocket).new(@logger.host, @logger.port) 35 | dont_allow(OpenSSL::SSL::SSLSocket).new 36 | mock.proxy(@logger).flush 37 | mock.proxy(@logger).log(Logger::INFO, @message) 38 | mock.proxy(@logger).write_on_socket(Logger::INFO, @message) 39 | @logger.add(Logger::INFO, @message) 40 | RR.verify 41 | end 42 | 43 | should "have sent the message to syslog using SSL with peer verification" do 44 | @logger = TcpSyslog.new( 45 | :ssl => { 46 | :cert => "/path/to/cert", 47 | :key => "/path/to/key", 48 | :ca_file => "/path/to/ca_file" 49 | } 50 | ) 51 | mock(@logger).ssl_socket{mock!.write(anything)} 52 | mock.proxy(@logger).flush 53 | mock.proxy(@logger).log(Logger::INFO, @message) 54 | mock.proxy(@logger).write_on_socket(Logger::INFO, @message) 55 | @logger.add(Logger::INFO, @message) 56 | RR.verify 57 | end 58 | 59 | end 60 | end 61 | 62 | context "A TcpSyslogger with auto_flushing set to 2" do 63 | setup do 64 | @logger = TcpSyslog.new(:auto_flushing => 2) 65 | end 66 | 67 | should "have set auto_flushing to 2" do 68 | assert_equal 2, @logger.auto_flushing 69 | end 70 | 71 | context "when adding a message" do 72 | setup do 73 | message = "info message" 74 | dont_allow(@logger).write_on_socket(Logger::INFO, message) 75 | @logger.info(message) 76 | end 77 | 78 | should "not send the message to syslog now" do 79 | RR.verify 80 | end 81 | 82 | context "and adding another message" do 83 | setup do 84 | message2 = "info message 2" 85 | RR.reset 86 | mock.proxy(@logger).write_on_socket(Logger::INFO, anything).twice 87 | @logger.info(message2) 88 | end 89 | 90 | should "have sent the messages to syslog" do 91 | RR.verify 92 | end 93 | end 94 | end 95 | end 96 | end 97 | -------------------------------------------------------------------------------- /tcp_syslog.gemspec: -------------------------------------------------------------------------------- 1 | # Generated by jeweler 2 | # DO NOT EDIT THIS FILE DIRECTLY 3 | # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' 4 | # -*- encoding: utf-8 -*- 5 | 6 | Gem::Specification.new do |s| 7 | s.name = "tcp_syslog" 8 | s.version = "1.0.3" 9 | 10 | s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 11 | s.authors = ["Philippe Lafoucri\303\250re"] 12 | s.date = "2011-11-27" 13 | s.description = "Rails syslog logger using TCP instead of UDP" 14 | s.email = "philippe.lafoucriere@gmail.com" 15 | s.extra_rdoc_files = [ 16 | "LICENSE.txt", 17 | "README.rdoc" 18 | ] 19 | s.files = [ 20 | ".document", 21 | "Changelog.md", 22 | "Gemfile", 23 | "LICENSE.txt", 24 | "README.rdoc", 25 | "Rakefile", 26 | "lib/tcp_syslog.rb", 27 | "tcp_syslog.gemspec", 28 | "test/helper.rb", 29 | "test/tcp_syslog_benchmark.rb", 30 | "test/test_tcp_syslog.rb" 31 | ] 32 | s.homepage = "http://github.com/tech-angels/tcp_syslog" 33 | s.licenses = ["MIT"] 34 | s.require_paths = ["lib"] 35 | s.rubygems_version = "1.8.10" 36 | s.summary = "Send logs to syslog using TCP instead of UDP" 37 | s.test_files = [ 38 | "test/helper.rb", 39 | "test/tcp_syslog_benchmark.rb", 40 | "test/test_tcp_syslog.rb" 41 | ] 42 | 43 | if s.respond_to? :specification_version then 44 | s.specification_version = 3 45 | 46 | if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then 47 | s.add_development_dependency(%q, [">= 0"]) 48 | s.add_development_dependency(%q, ["~> 0.6.0"]) 49 | s.add_development_dependency(%q, ["~> 1.0.0"]) 50 | s.add_development_dependency(%q, ["~> 1.5.1"]) 51 | s.add_development_dependency(%q, [">= 0"]) 52 | s.add_development_dependency(%q, ["~> 3.0.0"]) 53 | s.add_development_dependency(%q, [">= 0"]) 54 | s.add_development_dependency(%q, [">= 0"]) 55 | s.add_development_dependency(%q, [">= 0"]) 56 | s.add_runtime_dependency(%q, ["~> 3.0.0"]) 57 | s.add_runtime_dependency(%q, [">= 1.2.3"]) 58 | else 59 | s.add_dependency(%q, [">= 0"]) 60 | s.add_dependency(%q, ["~> 0.6.0"]) 61 | s.add_dependency(%q, ["~> 1.0.0"]) 62 | s.add_dependency(%q, ["~> 1.5.1"]) 63 | s.add_dependency(%q, [">= 0"]) 64 | s.add_dependency(%q, ["~> 3.0.0"]) 65 | s.add_dependency(%q, [">= 0"]) 66 | s.add_dependency(%q, [">= 0"]) 67 | s.add_dependency(%q, [">= 0"]) 68 | s.add_dependency(%q, ["~> 3.0.0"]) 69 | s.add_dependency(%q, [">= 1.2.3"]) 70 | end 71 | else 72 | s.add_dependency(%q, [">= 0"]) 73 | s.add_dependency(%q, ["~> 0.6.0"]) 74 | s.add_dependency(%q, ["~> 1.0.0"]) 75 | s.add_dependency(%q, ["~> 1.5.1"]) 76 | s.add_dependency(%q, [">= 0"]) 77 | s.add_dependency(%q, ["~> 3.0.0"]) 78 | s.add_dependency(%q, [">= 0"]) 79 | s.add_dependency(%q, [">= 0"]) 80 | s.add_dependency(%q, [">= 0"]) 81 | s.add_dependency(%q, ["~> 3.0.0"]) 82 | s.add_dependency(%q, [">= 1.2.3"]) 83 | end 84 | end 85 | 86 | -------------------------------------------------------------------------------- /lib/tcp_syslog.rb: -------------------------------------------------------------------------------- 1 | require 'socket' 2 | require 'syslog' 3 | require 'logger' 4 | require 'system_timer' 5 | require 'openssl' 6 | # TcpSyslog is used are a dead-simple replacement for 7 | # syslog ruby libs. None of them is able to send logs 8 | # to a remote server, and even less in TCP. 9 | # 10 | # Example: 11 | # 12 | # For rails (2.X) : 13 | # 14 | # config.logger = TcpSyslog.new(host => 'localhost') 15 | # 16 | # For more info about Syslog protocol, please refer to the RFC: 17 | # http://www.faqs.org/rfcs/rfc3164.html 18 | # 19 | # Parts taken frm SyslogLogger gem and ActiveSupport 20 | # 21 | class TcpSyslog < ActiveSupport::BufferedLogger 22 | include Logger::Severity 23 | 24 | # From 'man syslog.h': 25 | # LOG_EMERG A panic condition was reported to all processes. 26 | # LOG_ALERT A condition that should be corrected immediately. 27 | # LOG_CRIT A critical condition. 28 | # LOG_ERR An error message. 29 | # LOG_WARNING A warning message. 30 | # LOG_NOTICE A condition requiring special handling. 31 | # LOG_INFO A general information message. 32 | # LOG_DEBUG A message useful for debugging programs. 33 | 34 | # From logger rdoc: 35 | # FATAL: an unhandleable error that results in a program crash 36 | # ERROR: a handleable error condition 37 | # WARN: a warning 38 | # INFO: generic (useful) information about system operation 39 | # DEBUG: low-level information for developers 40 | 41 | # Maps Logger warning types to syslog(3) warning types. 42 | LOGGER_MAP = { 43 | :unknown => Syslog::LOG_ALERT, 44 | :fatal => Syslog::LOG_CRIT, 45 | :error => Syslog::LOG_ERR, 46 | :warn => Syslog::LOG_WARNING, 47 | :info => Syslog::LOG_INFO, 48 | :debug => Syslog::LOG_DEBUG 49 | } 50 | 51 | # Maps Logger log levels to their values so we can silence. 52 | LOGGER_LEVEL_MAP = {} 53 | 54 | LOGGER_MAP.each_key do |key| 55 | LOGGER_LEVEL_MAP[key] = Logger.const_get key.to_s.upcase 56 | end 57 | 58 | # Maps Logger log level values to syslog log levels. 59 | LEVEL_LOGGER_MAP = {} 60 | 61 | LOGGER_LEVEL_MAP.invert.each do |level, severity| 62 | LEVEL_LOGGER_MAP[level] = LOGGER_MAP[severity] 63 | end 64 | 65 | # Usage : 66 | # * +options+ : A hash with the following options 67 | # ** +host+ : defaults to 'localhost' 68 | # ** +port+ : defaults to '514' 69 | # ** +facility+ : defaults to user 70 | # ** +progname+ : defaults to 'rails' 71 | # ** +auto_flushing+ : number of messages to buffer before flushing 72 | # ** +ssl+ : defaults to nil 73 | # *** +cert : "/path/to/cert" 74 | # *** +key : "/path/to/key" 75 | # *** +ca_file : "/path/to/ca_file" 76 | # 77 | def initialize(options = {}) 78 | @level = LOGGER_LEVEL_MAP[options[:level]] || Logger::DEBUG 79 | @host = options[:host] || 'localhost' 80 | @port = options[:port] || '514' 81 | @facility = options[:facility] || Syslog::LOG_USER 82 | @progname = options[:progname] || 'rails' 83 | @buffer = Hash.new { |h,k| h[k] = [] } 84 | @socket = {} 85 | @auto_flushing = options[:auto_flushing] || 1 86 | @local_ip = local_ip 87 | @remove_ansi_colors = options[:remove_ansi_colors] || true 88 | @ssl = options[:ssl] 89 | return if defined? SYSLOG 90 | self.class.const_set :SYSLOG, true 91 | end 92 | 93 | 94 | # Log level for Logger compatibility. 95 | attr_reader :host, :port, :facility, :auto_flushing, :progname 96 | # Check ActiveSupport::BufferedLogger for other attributes 97 | 98 | # Almost duplicates Logger#add. 99 | def add(severity, message, progname = nil, &block) 100 | severity ||= Logger::UNKNOWN 101 | return if @level > severity 102 | message = clean(message || block.call) 103 | buffer << {:severity => severity, :body => clean(message)} 104 | auto_flush 105 | message 106 | end 107 | 108 | # In Logger, this dumps the raw message; the closest equivalent 109 | # would be Logger::UNKNOWN 110 | def <<(message) 111 | add(Logger::UNKNOWN, message) 112 | end 113 | 114 | def close 115 | flush 116 | socket.close 117 | @socket[Thread.current] = nil 118 | end 119 | 120 | # Flush buffered logs to Syslog 121 | def flush 122 | buffer.each do |message| 123 | log(message[:severity], message[:body]) 124 | end 125 | clear_buffer 126 | end 127 | 128 | def socket 129 | @socket[Thread.current] ||= new_socket 130 | end 131 | 132 | def ssl? 133 | !@ssl.nil? 134 | end 135 | 136 | protected 137 | 138 | def new_socket 139 | ssl? ? ssl_socket : tcp_socket 140 | end 141 | 142 | def tcp_socket 143 | TCPSocket.new(@host, @port) 144 | end 145 | 146 | def ssl_socket 147 | ssl_context = OpenSSL::SSL::SSLContext.new 148 | ssl_context.cert = OpenSSL::X509::Certificate.new(File.open(@ssl[:cert])) 149 | ssl_context.key = OpenSSL::PKey::RSA.new(File.open(@ssl[:key])) 150 | ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER 151 | ssl_context.ca_file = @ssl[:ca_file] 152 | ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context) 153 | ssl_socket.sync_close = true 154 | ssl_socket.connect 155 | ssl_socket 156 | end 157 | 158 | # Clean up messages so they're nice and pretty. 159 | def clean(message) 160 | message = message.to_s.dup 161 | message.strip! 162 | message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf) 163 | message.gsub!(/\e\[[^m]*m/, '') if @remove_ansi_colors # remove useless ansi color codes 164 | return message 165 | end 166 | 167 | # Returns current ip 168 | # (taken from http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/) 169 | def local_ip 170 | orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily 171 | 172 | UDPSocket.open do |s| 173 | s.connect '64.233.187.99', 1 174 | s.addr.last 175 | end 176 | ensure 177 | Socket.do_not_reverse_lookup = orig 178 | end 179 | 180 | # Log the message to syslog 181 | # This method is private, use the +add+ method instead 182 | def log(severity, msg) 183 | begin 184 | # Newline characters are not allowed in MSG part 185 | msg.split("\n").each do |line| 186 | write_on_socket(severity, line) 187 | end 188 | rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EPIPE, Timeout::Error, OpenSSL::SSL::SSLError => e 189 | # can't log anything, stop trying 190 | @socket[Thread.current] = nil 191 | end 192 | end 193 | 194 | # actually write on the tcp socket 195 | def write_on_socket(severity, msg) 196 | SystemTimer.timeout_after(1) do 197 | # Build syslog packet 198 | packet = "<#{@facility + LEVEL_LOGGER_MAP[severity]}>#{Time.now.strftime("%b %e %H:%M:%S")} #{@local_ip} [#{@progname}]: #{msg}\n" 199 | # Max size of a packet cannot be greater than 1024 bytes 200 | if packet.size > 1024 # FIXME1.9: use bytesize, fix following 2 lines 201 | socket.write(packet[0..1022] + "\n") 202 | write_on_socket severity, packet[1023..-1] 203 | else 204 | socket.write(packet) 205 | end 206 | end 207 | end 208 | 209 | end 210 | --------------------------------------------------------------------------------