1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

feature #924: Add CloudLogger module

This commit is contained in:
Daniel Molina 2012-02-27 17:34:49 +01:00
parent e97040d3e5
commit 8db484ce41
2 changed files with 54 additions and 4 deletions

View File

@ -39,11 +39,12 @@ class CloudAuth
# Tokens will be generated if time > EXPIRE_TIME - EXPIRE_MARGIN
EXPIRE_MARGIN = 300
attr_reader :client, :token
attr_reader :client, :token, :logger
# conf a hash with the configuration attributes as symbols
def initialize(conf)
def initialize(conf, logger=nil)
@conf = conf
@logger = logger
@token_expiration_time = Time.now.to_i + EXPIRE_DELTA

View File

@ -37,7 +37,7 @@ class CloudServer
##########################################################################
# Public attributes
##########################################################################
attr_reader :config
attr_reader :config, :logger
# Initializes the Cloud server based on a config file
# config_file:: _String_ for the server. MUST include the following
@ -45,9 +45,10 @@ class CloudServer
# AUTH
# VM_TYPE
# XMLRPC
def initialize(config)
def initialize(config, logger=nil)
# --- Load the Cloud Server configuration file ---
@config = config
@logger = logger
end
#
# Prints the configuration of the server
@ -82,3 +83,51 @@ class CloudServer
return false
end
end
module CloudLogger
require 'logger'
DEBUG_LEVEL = [
Logger::ERROR, # 0
Logger::WARN, # 1
Logger::INFO, # 2
Logger::DEBUG # 3
]
# Mon Feb 27 06:02:30 2012 [Clo] [E]: Error message example
MSG_FORMAT = %{%s [Clo] [%s]: %s\n}
# Mon Feb 27 06:02:30 2012
DATE_FORMAT = "%a %b %d %H:%M:%S %Y"
# Patch logger class to be compatible with Rack::CommonLogger
class ::Logger
def write(msg)
info msg.chop
end
end
def enable_logging(path=nil, debug_level=3)
path ||= $stdout
logger = ::Logger.new(path)
logger.level = DEBUG_LEVEL[debug_level]
logger.formatter = proc do |severity, datetime, progname, msg|
MSG_FORMAT % [
datetime.strftime(DATE_FORMAT),
severity[0..0],
msg ]
end
# Add the logger instance to the Sinatra settings
set :logger, logger
# Use the logger instance in the Rack methods
use Rack::CommonLogger, logger
helpers do
def logger
settings.logger
end
end
end
end