Skip to content

Commit

Permalink
CDS is now able to use a loging config file, similar to pulp server
Browse files Browse the repository at this point in the history
  • Loading branch information
jwmatthews committed Oct 7, 2013
1 parent f9d840e commit 438e944
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 39 deletions.
89 changes: 89 additions & 0 deletions etc/pulp/logging/cds.cfg
@@ -0,0 +1,89 @@
# Refer to python docs for information on format of this file
# http://docs.python.org/library/logging.config.html#configuration-file-format
#
# Quick Summary:
# To change logging options.
# Look for 'qualname' in a logger_* section
# 'qualname' represents the module name in the python file
# Example: pulp.server.api.repo_sync would be controlled by
# qualname: pulp.server.api.repo_sync
# as well as 'qualname: pulp.server.api'
#
# If you want to add a new section
# 1) Add an entry to 'keys' under [loggers] for MODULENAME
# 2) Add a new [logger_MODULENAME]
# 3) Add a 'propagate: 0' to [logger_MODULENAME]
# This will ensure the log messages are handled as you want
# and don't reach any higher up loggers
#
# Note:
# Python 2.4 has an issue with spaces between commas
[loggers]
keys: root,pulp,gofer,grinder,qpid

[logger_root]
# NOTSET for 'root' means it will allow
# any log statements that reach here to be displayed
level: NOTSET
handlers: pulp_file,console

[logger_pulp]
level: INFO
qualname: pulp
# Propagate 0 stops messages for this logger from being
# logged a another time by a higher logger in the hierachy
# i.e. 'propagate 0' stops these messages from being logged multiple times
propagate: 0
handlers: pulp_file,console

[logger_gofer]
level: INFO
propagate: 0
qualname: gofer
handlers: pulp_file

[logger_qpid]
level: INFO
propagate: 0
qualname: qpid
handlers: pulp_file

[logger_grinder]
level: INFO
propagate: 0
qualname: grinder
handlers: grinder_file

[formatters]
keys: detailed,simple

[formatter_simple]
format: %(asctime)s %(name)s:%(levelname)s: %(message)s

[formatter_detailed]
# Note: (funcName)s is not present in Python 2.4
# format: %(asctime)s %(process)d:%(thread)d: %(name)s:%(levelname)s: %(module)s:%(funcName)s:%(lineno)d %(message)s
format: %(asctime)s %(process)d:%(thread)d: %(name)s:%(levelname)s: %(module)s:%(lineno)d %(message)s

[handlers]
# No spaces, python 2.4 has an issue if you have spaces
keys: console,pulp_file,grinder_file

[handler_console]
level: ERROR
class: StreamHandler
args: []
formatter: simple

[handler_pulp_file]
level: DEBUG
class: handlers.RotatingFileHandler
args: ['/var/log/pulp-cds/pulp.log', 'a', 10000000, 3]
formatter: detailed

[handler_grinder_file]
level: DEBUG
class: handlers.RotatingFileHandler
args: ['/var/log/pulp-cds/grinder.log', 'a', 10000000, 3]
formatter: detailed

2 changes: 1 addition & 1 deletion pulp.spec
Expand Up @@ -436,7 +436,6 @@ fi
%attr(750, apache, apache) %{_sysconfdir}/pulp/admin
%attr(640, apache, apache) %{_sysconfdir}/pulp/*.conf
%attr(640, apache, apache) %{_sysconfdir}/pulp/*/*.conf
%attr(640, apache, apache) %{_sysconfdir}/pulp/logging/*.cfg
%ghost %{_sysconfdir}/yum.repos.d/pulp.repo
%attr(-, apache, apache) /srv/pulp/webservices.wsgi
%attr(-, apache, apache) /srv/pulp/repo_auth.wsgi
Expand All @@ -457,6 +456,7 @@ fi
%doc
%{python_sitelib}/pulp/__init__.*
%{python_sitelib}/pulp/common/
%attr(640, apache, apache) %{_sysconfdir}/pulp/logging/*.cfg

# -- files - pulp client lib -----------------------------------------------------

Expand Down
48 changes: 11 additions & 37 deletions src/pulp/cds/cdslib.py
Expand Up @@ -23,51 +23,25 @@

# -- constants ---------------------------------------------------------------

LOGPATH = '/var/log/pulp-cds/gofer.log'
REPO_LIST_FILENAME = '.cds_repo_list'
TIME = '%(asctime)s'
LEVEL = ' [%(levelname)s]'
THREAD = '[%(threadName)s]'
FUNCTION = ' %(funcName)s()'
FILE = ' @ %(filename)s'
LINE = ':%(lineno)d'
MSG = ' - %(message)s'

if sys.version_info < (2,5):
FUNCTION = ''

FMT = \
''.join((TIME,
LEVEL,
THREAD,
FUNCTION,
FILE,
LINE,
MSG,))

log = None

# -- public ------------------------------------------------------------------

def loginit(path=LOGPATH):
def loginit(log_cfg_file=None):
'''
Init log if (once).
@param path: The absolute path to the log file.
Configures the Python's logger based on a configuration file.
@param log_cfg_file: The absolute path to a logging configuration file
@type path: str
@return: The logger.
@return: None
@rtype: Logger
'''
global log
if log is None:
logdir = os.path.dirname(path)
if not os.path.exists(logdir):
os.makedirs(logdir)
log = logging.getLogger(__name__)
handler = logging.FileHandler(path)
handler.setFormatter(logging.Formatter(FMT))
log.addHandler(handler)
log.setLevel(logging.DEBUG)
return log
if not os.access(log_cfg_file, os.R_OK):
raise RuntimeError("Unable to read log configuration file: %s" % (log_cfg_file))
logging.config.fileConfig(log_cfg_file)
log = logging.getLogger(__name__)

def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
Expand All @@ -84,7 +58,9 @@ def __init__(self, config):
self.repo_cert_utils = RepoCertUtils(self.config)
self.protected_repo_utils = ProtectedRepoUtils(self.config)

loginit()
log_config_file = self.config.get('logs', 'config')
loginit(log_cfg_file=log_config_file)


def initialize(self):
'''
Expand Down Expand Up @@ -343,7 +319,6 @@ def _sync_repo(self, base_url, repo, remove_old_versions=False, num_old_pkgs_kee


url = '%s/%s' % (base_url, repo['relative_path'])
log.info('Synchronizing repo at [%s]' % url)
repo_path = os.path.join(content_base, 'repos', repo['relative_path'])

if not os.path.exists(repo_path):
Expand Down Expand Up @@ -387,7 +362,6 @@ def _sync_repo(self, base_url, repo, remove_old_versions=False, num_old_pkgs_kee
remove_old=remove_old_versions, numOldPackages=num_old_pkgs_keep)
report = fetch.fetchYumRepo(repo_path, verify_options=verify_options)

log.info('Successfully finished synccing [%s]' % url)
log.info("CDS Sync report for [%s]: %s" % (url, report))
return report

Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_cds_plugin.py
Expand Up @@ -31,7 +31,7 @@
TEST_LOCK_FILE = '/tmp/cds-plugin-storage-lock'

# setup logging
loginit(os.path.join(ROOTDIR, 'cds.log'))
loginit(os.path.abspath(os.path.dirname(__file__)) + '/../../etc/pulp/logging/unit_tests.cfg')

class TestCdsPlugin(testutil.PulpAsyncTest):

Expand Down

0 comments on commit 438e944

Please sign in to comment.