egonbraun/logmsg

View on GitHub
bin/logmsg

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'logmsg'
require 'getoptlong'

# ##############################################################################
# CONSTANTS
# ##############################################################################

# Default severity of the log message
# @private
DEFAULT_SEVERITY = 'info'

# Default namespace so it can load proper settings from the settings file
# @private
DEFAULT_NAMESPACE = 'logmsg_cli'

# ##############################################################################
# METHODS
# ##############################################################################

def usage
  puts <<-EOF
logmsg [OPTIONS] ... MESSAGE

-h, --help:
   show this help message

--config [path], -c [path]:
   set the configuration file (in YAML) to be used

--namespace [name], -n [name]:
   use name as the identifier of the program. Default is 'logmsg_cli'

 --severity [severity], -s [severity]
  set the severity fo the message, Default is 'info'

MESSAGE: The actual log message to output
  EOF
end

# ##############################################################################

severity = DEFAULT_SEVERITY
namespace = DEFAULT_NAMESPACE
cfg_file = nil
message  = nil

options = GetoptLong.new(
  ['--help',      '-h', GetoptLong::NO_ARGUMENT],
  ['--severity',  '-s', GetoptLong::REQUIRED_ARGUMENT],
  ['--namespace', '-n', GetoptLong::REQUIRED_ARGUMENT],
  ['--config',    '-c', GetoptLong::REQUIRED_ARGUMENT]
)

options.each do |opt, arg|
  case opt
  when '--help'
    usage
    exit 0
  when '--severity'
    severity = arg.downcase
  when '--namespace'
    namespace = arg
  when '--config'
    cfg_file = arg
  end
end

if ARGV.length < 1
  puts 'Missing log message (try --help)'
  exit 0
else
  message = ARGV.join(' ')
end

logmsg = Logmsg::LogMsg.new(namespace, cfg_file)

case severity
when 'debug'
  logmsg.debug(message)
when 'info'
  logmsg.info(message)
when 'warn'
  logmsg.warn(message)
when 'error'
  logmsg.error(message)
when 'fatal'
  logmsg.fatal(message)
else
  logmsg.unknown(message)
end

logmsg.close