Skip to content

Commit

Permalink
Corrected log configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Jul 5, 2024
1 parent d6677f7 commit 823493d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 50 deletions.
2 changes: 1 addition & 1 deletion local-run/with-jupyter-notebook/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
- jupyter_volume:/home/jovyan/:rw
- python_lib:/home/json-stream/python/lib:ro
- ${USER_DATA_DIR}:/home/jovyan/user-data/:rw
- ./json-stream-provider/custom.json:/var/th2/config/custom.json:ro
- ./json-stream-provider:/var/th2/config:ro
networks:
- th2_network

Expand Down
22 changes: 22 additions & 0 deletions local-run/with-jupyter-notebook/json-stream-provider/log4py.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[loggers]
keys=root

[handlers]
keys=consoleHandler

[formatters]
keys=formatter

[logger_root]
level=INFO
handlers=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=formatter
args=(sys.stdout,)

[formatter_formatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S
34 changes: 34 additions & 0 deletions log_configuratior.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import logging.config
import os

log4py_file = '/var/th2/config/log4py.conf'
def configureLogging():
if os.path.exists(log4py_file):
logging.config.fileConfig(log4py_file, disable_existing_loggers=False)
logging.getLogger(__name__).info(f'Logger is configured by {log4py_file} file')
else:
default_logging_config = {
'version': 1,
'formatters': {
'default': {
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'default',
'level': 'DEBUG',
'stream': 'ext://sys.stdout'
},
},
'root': {
'handlers': ['console'],
'level': 'DEBUG',
},
}
logging.config.dictConfig(default_logging_config)
logging.getLogger(__name__).info('Logger is configured by default')


20 changes: 9 additions & 11 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,30 @@
# limitations under the License.

import os

from log_configuratior import configureLogging

os.system('pip list')

import subprocess
import sys
import papermill as pm
from aiohttp.web_request import Request
from aiohttp import web
from aiojobs.aiohttp import setup, spawn
from aiohttp_swagger import *
from glob import glob
import json
import datetime
import asyncio
from argparse import ArgumentParser
import logging
import logging.config

serverStatus: str = 'idle'
notebooksDir: str = '/home/jupyter-notebook/'
resultsDir: str = '/home/jupyter-notebook/results/'
logDir: str = '/home/jupyter-notebook/logs/'
tasks: dict = {}
logger: logging.Logger

configureLogging()
logger: logging.Logger = logging.getLogger('j-sp')

def notebooksReg(path):
return path + '/*.ipynb'
Expand Down Expand Up @@ -75,7 +76,7 @@ def readConf(path: str):
if logDir:
createDir(logDir)
except Exception as e:
logger.debug(e)
logger.error(f"Read '{path}' configuration failure", e)


async def reqStatus(req: Request):
Expand Down Expand Up @@ -263,15 +264,14 @@ async def launchNotebook(input, arguments, file_name, task_id):
with pm.utils.chdir(input[:input.rfind('/')]):
input = input[input.rfind('/')+1:]
pm.execute_notebook(input, logOut, arguments)
logger.debug('successfully launched notebook {input}'.format(input=input))
logger.info('successfully launched notebook {input}'.format(input=input))
if tasks.get(task_id):
tasks[task_id] = {
'status': 'success',
'result': arguments.get('output_path')
}
except Exception as error:
logger.info('failed to launch notebook {input}'.format(input=input))
logger.debug(error)
logger.error('failed to launch notebook {input}'.format(input=input), error)
if tasks.get(task_id):
tasks[task_id] = {
'status': 'failed',
Expand Down Expand Up @@ -430,8 +430,6 @@ async def reqStop(req: Request):
return web.HTTPOk()

if __name__ == '__main__':
logging.basicConfig(filename='var/th2/config/log4py.conf', level=logging.DEBUG)
logger=logging.getLogger('th2_common')
parser = ArgumentParser()
parser.add_argument('config')
path = vars(parser.parse_args()).get('config')
Expand Down
38 changes: 0 additions & 38 deletions var/th2/config/log4py.conf

This file was deleted.

0 comments on commit 823493d

Please sign in to comment.