Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Allow instantiation with connection string #27

Open
DannyBen opened this issue Mar 15, 2019 · 0 comments
Open

Suggestion: Allow instantiation with connection string #27

DannyBen opened this issue Mar 15, 2019 · 0 comments

Comments

@DannyBen
Copy link
Contributor

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant