Skip to content

Commit

Permalink
Merge branch 'fix_log' into 'master'
Browse files Browse the repository at this point in the history
Make log creation on apprecommender run

Originally, the log file was being generated once Apprecommender was
initialized. However, this approach was not very good considering
multiple users on the system. Therefore, a log is created once a user
executes AppRecommender main script

See merge request !25
  • Loading branch information
LucianoPC committed Aug 1, 2016
2 parents 75d8adb + 482be7c commit 06451e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions app_recommender.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ verbose = 0
# base_dir = /var/lib/apprecommender/
base_dir = ~/.app-recommender/
user_data_dir = user_data/
output = apprec.log
# filters for valid packages
filters_dir = filters
pkgs_filter = desktopapps
Expand Down
25 changes: 8 additions & 17 deletions apprecommender/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ def __init__(self):
['/etc/apprecommender/recommender.conf',
os.path.expanduser('~/.app_recommender.rc'),
os.path.expanduser('app_recommender.cfg')])

log_path = "/home/{}/.app-recommender/"

if os.getenv('SUDO_USER'):
self.output = log_path.format(os.getenv("SUDO_USER"))
elif os.getenv('USER'):
self.output = log_path.format(os.getenv("USER"))
self.home_dir = os.path.expanduser('~/.app-recommender')

except (MissingSectionHeaderError), err:
logging.error("Error in config file syntax: %s", str(err))
Expand All @@ -60,6 +54,7 @@ def __init__(self):
# data_source options
# self.base_dir = '/var/lib/apprecommender'
self.base_dir = os.path.expanduser('~/.app-recommender')
self.output = os.path.join(self.home_dir, 'apprec.log')
self.user_data_dir = os.path.join(self.base_dir, "user_data/")
# general options
self.debug = 0
Expand Down Expand Up @@ -112,7 +107,6 @@ def __init__(self):
self.popcon_profiling = "full"

self.load_config_file()
self.set_logger()
self.initialized = 1
logging.info("Basic config")

Expand All @@ -135,6 +129,9 @@ def load_config_file(self):
self.debug = int(self.read_option('general', 'verbose'))
self.base_dir = os.path.expanduser(
self.read_option('data_sources', 'base_dir'))
self.output = os.path.join(
self.home_dir, self.read_option('data_sources',
'output'))
self.user_data_dir = os.path.join(
self.base_dir, self.read_option('data_sources',
'user_data_dir'))
Expand Down Expand Up @@ -193,7 +190,7 @@ def set_logger(self):
Configure application logger and log level.
"""
self.logger = logging.getLogger('') # root logger is used by default
self.logger.setLevel(logging.INFO)
self.logger.setLevel(logging.WARNING)

if self.debug == 1:
log_level = logging.DEBUG
Expand All @@ -208,19 +205,13 @@ def set_logger(self):
console_handler.setLevel(log_level)
self.logger.addHandler(console_handler)

if not os.path.exists(self.output):
os.makedirs(self.output, mode=0777)
os.chmod(self.output, 0777)

self.output += "apprec.log"
if not os.path.exists(self.home_dir):
os.makedirs(self.home_dir)

file_handler = logging.handlers.RotatingFileHandler(self.output,
maxBytes=50000000,
backupCount=5)

if os.getenv('SUDO_USER'):
os.chmod(self.output, 0777)

log_format = '%(asctime)s %(levelname)-8s %(message)s'
file_handler.setFormatter(logging.Formatter(
log_format, datefmt='%Y-%m-%d %H:%M:%S'))
Expand Down

0 comments on commit 06451e8

Please sign in to comment.