Commit 2ed7c21f authored by Yuanle Song's avatar Yuanle Song
Browse files

v0.17.3 make bootstrap-dev no longer requires sudo

now log dir is created in runtime when importing configlogger.py
if user doesn't have perm, just use a basic logger config.
This makes code run in CI environment easier.
parent 165ccbc5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
(defproject python/lein-template "0.17.2"
(defproject python/lein-template "0.17.3"
  :description "lein template for a python project"
  :repositories [["snapshots"
                  {:url "http://devserv.game.yy.com/nexus/content/repositories/snapshots"
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ logdir:
	fi
bootstrap: venv requirements
bootstrap-dev: REQUIREMENTS += $(DEV_REQUIREMENTS)
bootstrap-dev: venv requirements logdir
bootstrap-dev: venv requirements
install: bootstrap
	$(PYTHON) setup.py -q install
uninstall: bootstrap
+37 −1
Original line number Diff line number Diff line
@@ -8,11 +8,47 @@ config logger
from __future__ import (absolute_import, division, print_function,
                        unicode_literals, with_statement)

import os.path
import logging

from logging.config import fileConfig
from pkg_resources import resource_filename


logging.captureWarnings(True)
def load_logger_config():
    """if /var/log/{{name}} exist, load default logger.conf.

    otherwise, try to create it directly.
    if that fails, try to create it with sudo.
    if that fails, use basic logger config.

    """
    logdir = "/var/log/{{name}}/"
    if os.path.exists(logdir):
        fileConfig(resource_filename("{{name}}", "logger.conf"))
        return

    try:
        os.makedirs(logdir)
        fileConfig(resource_filename("{{name}}", "logger.conf"))
        return
    except OSError as _:
        pass

    import subprocess
    exit_code = subprocess.call(["sudo", "mkdir", "-p", logdir])
    if exit_code == 0:
        fileConfig(resource_filename("{{name}}", "logger.conf"))
        return

    TESTING = os.getenv("PYTEST") == "1"
    level = logging.DEBUG if TESTING else logging.INFO
    logging.basicConfig(
        format='%(asctime)s [%(module)s] %(levelname)-8s %(message)s',
        level=level)
    logging.info("Create log dir %s failed. Using basic config, level=%s",
                 logdir, "DEBUG" if TESTING else "INFO")


logging.captureWarnings(True)
load_logger_config()