Commit 7186416d authored by mouadh's avatar mouadh

fix logs

parent 91f0169d
from __future__ import absolute_import, division, print_function
import logging
import os
from logging.handlers import RotatingFileHandler
class Logs:
"""
write server logs, it can be:
- mdx queries
- DataFrame results
- xmla requests and responses
:file_name: file name in which you want to write the logs
"""
def __init__(self, file_name):
self.file_name = file_name + ".log"
self.root_path = self._create_log_file()
def _create_log_file(self):
path = os.path.abspath(__file__)
path = path.lstrip(os.sep)
root = path[:path.index(os.sep)]
# TODO Fix this prob with c: (
# http://stackoverflow.com/questions/2422798/python-os-path-join-on-windows)
if not os.path.exists(os.path.join(root + '\\', 'logs')):
os.makedirs(os.path.join(root + '\\', 'logs'))
root = os.path.join(root + '\\', 'logs')
return root
def write_log(self, msg):
"""
Write logs messages to file
:param msg: message to write
"""
# Creation of the logger object that will serve us to write in the logs
logger = logging.getLogger()
# We set the logger level to DEBUG, so it writes everything
logger.setLevel(logging.DEBUG)
# Creation of a formatter that will add time, level
# Of each message when writing a message in the log
formatter = logging.Formatter(
'%(asctime)s :: %(levelname)s :: %(message)s')
# Creation of a handler which will redirect message of the log to
# file in append mode, with 1 backup and max size of 1MB
file_handler = RotatingFileHandler(
os.path.join(self.root_path, self.file_name), 'a', 1000000, 1)
# We put the level on DEBUG, we tell him that he must use the formatter
# Created earlier and add this handler to the logger
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
# Creation of a second handler that will redirect each log
# On the console
# Steam_handler = logging.StreamHandler ()
# Steam_handler.setLevel (logging.DEBUG)
# Logger.addHandler (steam_handler)
# It's time to spam your code with logs everywhere:
logger.info(msg)
# logger.warning('Testing %s', 'foo')
...@@ -236,11 +236,11 @@ def start_server(write_on_file=False): ...@@ -236,11 +236,11 @@ def start_server(write_on_file=False):
# TODO FIX it with os # TODO FIX it with os
if write_on_file: if write_on_file:
home_directory = expanduser("~") home_directory = expanduser("~")
if not os.path.isdir(os.path.join(home_directory, 'logs')): if not os.path.isdir(os.path.join(home_directory,'olapy-data', 'logs')):
os.makedirs(os.path.join(home_directory, 'logs')) os.makedirs(os.path.join(home_directory,'olapy-data', 'logs'))
logging.basicConfig( logging.basicConfig(
level=logging.DEBUG, level=logging.DEBUG,
filename=os.path.join(home_directory, 'logs', 'xmla.log')) filename=os.path.join(home_directory,'olapy-data', 'logs', 'xmla.log'))
else: else:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG) logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
......
...@@ -3,6 +3,7 @@ from __future__ import absolute_import, division, print_function ...@@ -3,6 +3,7 @@ from __future__ import absolute_import, division, print_function
import logging import logging
import os import os
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
from os.path import expanduser
class Logs: class Logs:
...@@ -12,15 +13,13 @@ class Logs: ...@@ -12,15 +13,13 @@ class Logs:
self.root_path = self._create_log_file() self.root_path = self._create_log_file()
def _create_log_file(self): def _create_log_file(self):
path = os.path.abspath(__file__) home_directory = expanduser("~")
path = path.lstrip(os.sep) location = os.path.join(home_directory, 'olapy-data', 'logs')
root = path[:path.index(os.sep)]
# TODO Fix this prob with c: ( if not os.path.exists(location):
# http://stackoverflow.com/questions/2422798/python-os-path-join-on-windows) os.makedirs(location)
if not os.path.exists(os.path.join(root + '\\', 'logs')):
os.makedirs(os.path.join(root + '\\', 'logs')) return location
root = os.path.join(root + '\\', 'logs')
return root
def write_log(self, msg): def write_log(self, msg):
# Creation of the logger object that will serve us to write in the logs # Creation of the logger object that will serve us to write in the logs
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment