remote_syslog_logger icon indicating copy to clipboard operation
remote_syslog_logger copied to clipboard

Suggestion: Allow instantiation with connection string

Open DannyBen opened this issue 5 years ago • 0 comments

How do we feel about allowing instantiation with a connection string?

I find myself instantiating a RemoteSyslogLogger by using several lines of code, instead of something simpler - like:

logger = RemoteSyslogLogger.new "syslog://system:[email protected]:514"

This will not only make things shorter and easier, but will have the added bonus of allowing to simply use an environment variable as the connection string, like we do with redis, database and others.

logger = RemoteSyslogLogger.new ENV['SYSLOG_URL']

Here is my monkey patch proof of concept:

require 'remote_syslog_logger'
require 'uri'

module RemoteSyslogLogger
  class << self
    alias_method :original_new, :new

    def new(*args)
      args = ['syslog://localhost:514'] if args.empty?
      if args.count == 1 and args.first.is_a? String
        uri = URI args.first
        original_new uri.host, uri.port, local_hostname: uri.user, program: uri.password
      else
        original_new *args
      end
    end
  end
end

logger0 = RemoteSyslogLogger.new '127.0.0.1', '514', local_hostname: 'myhost', program: 'myprog'
logger1 = RemoteSyslogLogger.new "syslog://system:[email protected]:514"
logger2 = RemoteSyslogLogger.new "syslog://127.0.0.1:514"
logger3 = RemoteSyslogLogger.new


logger0.info "Tadaaa 0"
logger1.info "Tadaaa 1"
logger2.info "Tadaaa 2"
logger3.info "Tadaaa 3"

DannyBen avatar Mar 15 '19 20:03 DannyBen