New logging system.

authc
Karchnu 2020-08-24 17:36:22 +02:00
parent ecc509ae24
commit bd9c196cc6
1 changed files with 39 additions and 20 deletions

View File

@ -11,6 +11,26 @@ require "grok"
require "./authd.cr" require "./authd.cr"
class Context
class_property verbosity = 1
end
class Log
def self.debug(message)
STDOUT << ":: ".colorize(:green) << message.colorize(:white) << "\n" if ::Context.verbosity > 2
end
def self.info(message)
STDOUT << ":: ".colorize(:blue) << message.colorize(:white) << "\n" if ::Context.verbosity > 1
end
def self.warning(message)
STDERR << "?? ".colorize(:yellow) << message.colorize(:yellow) << "\n" if ::Context.verbosity > 0
end
def self.error(message)
STDERR << "!! ".colorize(:red) << message.colorize(:red) << "\n" if ::Context.verbosity > 0
end
end
extend AuthD extend AuthD
class AuthD::Service class AuthD::Service
@ -416,10 +436,10 @@ class AuthD::Service
pattern =~ full_name.as_s pattern =~ full_name.as_s
end end
end end
puts "#{u.login} matches #{pattern}" Log.debug "#{u.login} matches #{pattern}"
matching_users << u.to_public matching_users << u.to_public
else else
puts "#{u.login} doesn't match #{pattern}" Log.error "#{u.login} doesn't match #{pattern}"
end end
end end
@ -499,14 +519,6 @@ class AuthD::Service
@users_per_uid.get? token_payload.uid.to_s @users_per_uid.get? token_payload.uid.to_s
end end
def info(message)
STDOUT << ":: ".colorize(:green) << message.colorize(:white) << "\n"
end
def error(message)
STDOUT << "!! ".colorize(:red) << message.colorize(:red) << "\n"
end
def run def run
## ##
# Provides a JWT-based authentication scheme for service-specific users. # Provides a JWT-based authentication scheme for service-specific users.
@ -515,19 +527,19 @@ class AuthD::Service
server.timer = 30000 # 30 seconds server.timer = 30000 # 30 seconds
server.loop do |event| server.loop do |event|
if event.is_a? IPC::Exception if event.is_a? IPC::Exception
puts "oh no" Log.error "IPC::Exception"
pp! event pp! event
next next
end end
case event case event
when IPC::Event::Timer when IPC::Event::Timer
info "Timer" Log.debug "Timer"
when IPC::Event::MessageReceived when IPC::Event::MessageReceived
begin begin
request = Request.from_ipc(event.message).not_nil! request = Request.from_ipc(event.message).not_nil!
info "<< #{request.class.name.sub /^Request::/, ""}" Log.info "<< #{request.class.name.sub /^Request::/, ""}"
response = handle_request request response = handle_request request
@ -535,16 +547,16 @@ class AuthD::Service
server.send event.fd, response server.send event.fd, response
rescue e : MalformedRequest rescue e : MalformedRequest
error "#{e.message}" Log.error "#{e.message}"
error " .. type was: #{e.ipc_type}" Log.error " .. type was: #{e.ipc_type}"
error " .. payload was: #{e.payload}" Log.error " .. payload was: #{e.payload}"
response = Response::Error.new e.message response = Response::Error.new e.message
rescue e rescue e
error "#{e.message}" Log.error "#{e.message}"
response = Response::Error.new e.message response = Response::Error.new e.message
end end
info ">> #{response.class.name.sub /^Response::/, ""}" Log.info ">> #{response.class.name.sub /^Response::/, ""}"
end end
end end
end end
@ -595,6 +607,13 @@ begin
read_only_profile_keys.push key read_only_profile_keys.push key
end end
parser.on "-v verbosity",
"--verbosity level",
"Verbosity level. From 0 to 3. Default: 1" do |v|
Context.verbosity = v.to_i
end
parser.on "-h", "--help", "Show this help" do parser.on "-h", "--help", "Show this help" do
puts parser puts parser
@ -611,9 +630,9 @@ begin
authd.read_only_profile_keys = read_only_profile_keys authd.read_only_profile_keys = read_only_profile_keys
end.run end.run
rescue e : OptionParser::Exception rescue e : OptionParser::Exception
STDERR.puts e.message Log.error e.message
rescue e rescue e
STDERR.puts "exception raised: #{e.message}" Log.error "exception raised: #{e.message}"
e.backtrace.try &.each do |line| e.backtrace.try &.each do |line|
STDERR << " - " << line << '\n' STDERR << " - " << line << '\n'
end end