Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled per-module logging level settings #41

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ virtualenv.py
tests/test-installation-environment
settings.py
Skype4Py
logging.conf

.vagrant
.idea
Expand Down
38 changes: 38 additions & 0 deletions logging.conf.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# http://docs.python.org/library/logging.config.html

[loggers]
keys=root,sevabot
#: Customize loggers for modules as below
#keys=root,sevabot,tasks

#: An example for customizing the logging level of tasks module
#[logger_tasks]
#level=DEBUG
#handlers=sevabot
#qualname=tasks

[handlers]
keys=sevabot

[formatters]
keys=sevabot

[logger_root]
level=NOTSET
handlers=

[logger_sevabot]
level=INFO
handlers=sevabot
qualname=sevabot

[handler_sevabot]
class=handlers.RotatingFileHandler
formatter=sevabot
args=('logs/sevabot.log', 'a', 1024 * 1024, 10, 'utf-8')
# filename, mode, maxBytes, backupCount, encoding

[formatter_sevabot]
# http://docs.python.org/library/logging.html
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
class=logging.Formatter
6 changes: 1 addition & 5 deletions modules/call.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/sevabot

# -*- coding: utf-8 -*-

"""
Expand All @@ -13,10 +12,7 @@
from sevabot.bot.stateful import StatefulSkypeHandler
from sevabot.utils import ensure_unicode

logger = logging.getLogger('Call')

# Set to debug only during dev
logger.setLevel(logging.INFO)
logger = logging.getLogger(__name__)

logger.debug('Call module level load import')

Expand Down
5 changes: 1 addition & 4 deletions modules/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
from sevabot.bot.stateful import StatefulSkypeHandler
from sevabot.utils import ensure_unicode, get_chat_id

logger = logging.getLogger("Tasks")

# Set to debug only during dev
logger.setLevel(logging.INFO)
logger = logging.getLogger(__name__)

logger.debug("Tasks module level load import")

Expand Down
2 changes: 1 addition & 1 deletion scripts/start-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ start() {
if [[ $? != '0' ]]; then
sleep 3
# http://devforum.skype.com/t5/Audio-Video/bt-audio-service-open-connect-failed-Connection-refused-111/td-p/303
skype > /dev/null 2&>1 &
skype > /dev/null 2>&1 &
else
echo "skype already running"
fi
Expand Down
19 changes: 1 addition & 18 deletions settings.py.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#: Known shared secret key in order to send messages over HTTP interface
SHARED_SECRET = "koskela"

Expand All @@ -21,21 +20,5 @@ HTTP_HOST = "localhost"
#: Which port we run our HTTP interface
HTTP_PORT = 5000

#: Set logging level (INFO or DEBUG)
#: This setting overrides --verbose option
#: LOG_LEVEL = "INFO"

#: Setup Python logging for Sevabot
#: Absolute path or relative to the settings file location
LOG_FILE = "logs/sevabot.log"

# http://docs.python.org/library/logging.html
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

#: Log rotation options
LOG_ROTATE_COUNT = 10

LOG_ROTATE_MAX_SIZE = 1024 * 1024

#: Log all HTTP requests for debugging purposes
DEBUG_HTTP = False
DEBUG_HTTP = False
36 changes: 5 additions & 31 deletions sevabot/frontend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"""
from __future__ import absolute_import, division, print_function, unicode_literals

import imp
import sys
import logging
import logging.handlers
import logging.config
import os

from flask import Flask
Expand All @@ -20,6 +19,7 @@

from sevabot.frontend import api
from sevabot.frontend.daemon import create_daemon
from sevabot.utils import load_settings

logger = logging.getLogger("sevabot")

Expand Down Expand Up @@ -58,42 +58,16 @@ def get_settings():
verbose=("Verbose debug output", 'flag', 'v', None, None),
daemon=("Start as a detached background process", 'flag', 'd', None, None),
)
def main(settings="settings.py", verbose=False, daemon=False):
def main(settings="settings.py", logconf="logging.conf", verbose=False, daemon=False):
"""
Application entry point.
"""

# Expose settings global module
try:
settings = imp.load_source("settings", settings)
except Exception:
import traceback
traceback.print_exc()
sys.exit("Could not load settings file: %s" % settings)
settings = load_settings(settings)

# Config logging

level = getattr(logging, getattr(settings, "LOG_LEVEL", "INFO").upper(), "INFO")

logging.basicConfig(level=level, stream=sys.stdout, format=settings.LOG_FORMAT)

# Setup logging file
if getattr(settings, "LOG_FILE", None):
if not settings.LOG_FILE.startswith("/"):
log_path = settings.LOG_FILE
else:
log_path = os.path.join(os.path.dirname(settings.__file__), settings.LOG_FILE)

formatter = logging.Formatter(settings.LOG_FORMAT)

hdlr = logging.handlers.RotatingFileHandler(log_path,
encoding="utf-8",
maxBytes=settings.LOG_ROTATE_MAX_SIZE,
backupCount=settings.LOG_ROTATE_COUNT)

hdlr.setFormatter(formatter)

logger.addHandler(hdlr)
logging.config.fileConfig(logconf, disable_existing_loggers=False)

logger.info("Starting sevabot")

Expand Down
18 changes: 18 additions & 0 deletions sevabot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""

import imp
import sys
import hashlib
import logging

Expand Down Expand Up @@ -58,3 +60,19 @@ def get_chat_id(chat):
m.update(chat.Name)
return m.hexdigest()


def load_settings(filename="settings.py"):
"""
Load the global settings file.

:return: Settings module.
"""

try:
settings = imp.load_source("settings", filename)
except Exception:
import traceback
traceback.print_exc()
sys.exit("Could not load settings file: %s" % settings)

return settings