Skip to content
Snippets Groups Projects
Commit f744c8a3 authored by Daniel Lehmberg's avatar Daniel Lehmberg
Browse files

replace logging with formatted prints

parent fac7da67
No related branches found
No related tags found
1 merge request!1388Integrate metrics to logtransfer
...@@ -56,10 +56,6 @@ def logtransfer(): ...@@ -56,10 +56,6 @@ def logtransfer():
from nomad.logtransfer import start_logtransfer_service from nomad.logtransfer import start_logtransfer_service
from nomad.statistics_logger import start_statistics_logger_process from nomad.statistics_logger import start_statistics_logger_process
# TODO: check why this was necessary to properly apply changes
# made a nomad.yaml file
config.load_config()
if not config.logstash.enabled: if not config.logstash.enabled:
raise RuntimeError( raise RuntimeError(
'To run the logtransfer service it is required that ' 'To run the logtransfer service it is required that '
......
...@@ -29,19 +29,16 @@ from datetime import datetime, timedelta ...@@ -29,19 +29,16 @@ from datetime import datetime, timedelta
from nomad import config from nomad import config
# It is important to make sure that logstash itself is disabled (otherwise it # For logtransfer no Python logging is used, but instead prints. The reason is that logging can interfere with
# can happen that logs from the logtransfer server are also included to the # Nomad logging and may be send to
# logs enable_log_print = True
config.logstash.enabled = False
# logger only for the logstash server (none of the logs should be transferred def log_print(message, level='INFO'):
# to the central Nomad instance) if enable_log_print:
server_logger = logging.getLogger(name='logtransfer_server') current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
server_logger.setLevel(logging.INFO) formatted_message = f'{level}: {current_time} {message}'
server_handler = logging.StreamHandler() print(formatted_message)
server_handler.setFormatter(logging.Formatter('%(levelname)s: %(asctime)s %(message)s', '%Y-%m-%d %H:%M:%S'))
server_logger.addHandler(server_handler)
def get_log_filepath(): def get_log_filepath():
...@@ -191,7 +188,7 @@ class Logtransfer: ...@@ -191,7 +188,7 @@ class Logtransfer:
all_rotated_logfiles = get_all_rotated_logfiles() all_rotated_logfiles = get_all_rotated_logfiles()
if len(all_rotated_logfiles) > 0: if len(all_rotated_logfiles) > 0:
server_logger.info( log_print(
f'collected files: ' f'collected files: '
f'{[os.path.basename(f) for f in all_rotated_logfiles]} in ' f'{[os.path.basename(f) for f in all_rotated_logfiles]} in '
f'directory {config.fs.tmp}' f'directory {config.fs.tmp}'
...@@ -203,7 +200,7 @@ class Logtransfer: ...@@ -203,7 +200,7 @@ class Logtransfer:
yield file_content, path yield file_content, path
else: else:
server_logger.info('No logfiles to submit.') log_print('No logfiles to submit.')
def _submit_logfile_to_central(self, logfile_content: bytes): def _submit_logfile_to_central(self, logfile_content: bytes):
...@@ -222,12 +219,12 @@ class Logtransfer: ...@@ -222,12 +219,12 @@ class Logtransfer:
if is_successful: if is_successful:
submitted_bytes = ret_request.json()['filesize'] submitted_bytes = ret_request.json()['filesize']
server_logger.info( log_print(
f'Successfully submitted logfile ({submitted_bytes} bytes) with HTTP status code ' f'Successfully submitted logfile ({submitted_bytes} bytes) with HTTP status code '
f'{ret_request.status_code} to central Oasis at {ret_request.url}.' f'{ret_request.status_code} to central Oasis at {ret_request.url}.'
) )
else: else:
server_logger.info( log_print(
f'Submission of logfiles to {central_post_federation_url} failed with HTTP ' f'Submission of logfiles to {central_post_federation_url} failed with HTTP '
f'status code {ret_request.status_code}. \n ' f'status code {ret_request.status_code}. \n '
'logfiles will be included again in next submission.' 'logfiles will be included again in next submission.'
...@@ -235,7 +232,7 @@ class Logtransfer: ...@@ -235,7 +232,7 @@ class Logtransfer:
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
is_successful = False is_successful = False
server_logger.info(f'HTTP connection to {central_post_federation_url} could not be established') log_print(f'HTTP connection to {central_post_federation_url} could not be established')
return is_successful return is_successful
...@@ -247,7 +244,7 @@ class Logtransfer: ...@@ -247,7 +244,7 @@ class Logtransfer:
def transfer_logs(self) -> None: def transfer_logs(self) -> None:
# (mypy does not recognize the superclass attribute) # (mypy does not recognize the superclass attribute)
server_logger.info( log_print(
'Start logtransfer thread with ' # type: ignore 'Start logtransfer thread with ' # type: ignore
f'{self.submit_interval=} sec | ' f'{self.submit_interval=} sec | '
f'{self.rotating_file.maxBytes=} bytes' f'{self.rotating_file.maxBytes=} bytes'
...@@ -269,24 +266,24 @@ class Logtransfer: ...@@ -269,24 +266,24 @@ class Logtransfer:
except Exception as e: except Exception as e:
if not self.raise_unexpected_exceptions: if not self.raise_unexpected_exceptions:
# do not kill the thread and hope it will fix for the next iteration # do not kill the thread and hope it will fix for the next iteration
server_logger.info(f'unexpected exception was raised \n{e}') log_print(f'unexpected exception was raised \n{e}')
else: else:
raise e raise e
next_submission = datetime.now() + timedelta(seconds=self.submit_interval) next_submission = datetime.now() + timedelta(seconds=self.submit_interval)
next_submission = next_submission.strftime('%Y-%m-%dT%H:%M:%S') # type: ignore next_submission = next_submission.strftime('%Y-%m-%dT%H:%M:%S') # type: ignore
server_logger.info(f'The next planned submission is at {next_submission}.') log_print(f'The next planned submission is at {next_submission}.')
self.reached_max_bytes_event.clear() self.reached_max_bytes_event.clear()
self.reached_max_bytes_event.wait(self.submit_interval) self.reached_max_bytes_event.wait(self.submit_interval)
is_notified = self.reached_max_bytes_event.is_set() is_notified = self.reached_max_bytes_event.is_set()
server_logger.info(f'Thread is waking up (is_notified={is_notified}).') log_print(f'Thread is waking up (is_notified={is_notified}).')
with self.rotating_file.rollover_lock: with self.rotating_file.rollover_lock:
# mypy does not recognize the superclass attribute # mypy does not recognize the superclass attribute
if self.rotating_file.stream.tell() != 0: # type: ignore if self.rotating_file.stream.tell() != 0: # type: ignore
server_logger.info( log_print(
'Perform rollover on logfile. stream position in ' # type: ignore 'Perform rollover on logfile. stream position in ' # type: ignore
f'bytes={self.rotating_file.stream.tell()}') f'bytes={self.rotating_file.stream.tell()}')
...@@ -295,7 +292,7 @@ class Logtransfer: ...@@ -295,7 +292,7 @@ class Logtransfer:
# where the logfiles are submitted and removed # where the logfiles are submitted and removed
self.rotating_file.doRollover() self.rotating_file.doRollover()
else: else:
server_logger.info( log_print(
'No rollover on logfile performed because ' 'No rollover on logfile performed because '
'rotating_file.stream.tell() is at position 0.' 'rotating_file.stream.tell() is at position 0.'
) )
...@@ -366,7 +363,7 @@ def _start_logstash_proxy_server(logger, host, port): ...@@ -366,7 +363,7 @@ def _start_logstash_proxy_server(logger, host, port):
logstash_proxy_server.timeout = 30 logstash_proxy_server.timeout = 30
with logstash_proxy_server as server: with logstash_proxy_server as server:
server_logger.info(f'Start logstash proxy server on host={host} port={port}.') log_print(f'Start logstash proxy server on host={host} port={port}.')
try: try:
# NOTE: don't use the serve_forever() method as it does not account # NOTE: don't use the serve_forever() method as it does not account
...@@ -374,12 +371,12 @@ def _start_logstash_proxy_server(logger, host, port): ...@@ -374,12 +371,12 @@ def _start_logstash_proxy_server(logger, host, port):
while True: while True:
server.handle_request() server.handle_request()
except Exception as e: except Exception as e:
server_logger.info('logstash proxy server shutdown unexpectedly') log_print('logstash proxy server shutdown unexpectedly')
raise e raise e
def _initialize_logger_and_handler(): def _initialize_logger_and_handler():
server_logger.info( log_print(
f'Initialize logger and handler. Location of intermediate logfiles before ' f'Initialize logger and handler. Location of intermediate logfiles before '
f'submission:\n{get_log_filepath()}' f'submission:\n{get_log_filepath()}'
) )
...@@ -410,7 +407,7 @@ def start_logtransfer_service(host=None, port=None): ...@@ -410,7 +407,7 @@ def start_logtransfer_service(host=None, port=None):
left None, Nomad config is taken. left None, Nomad config is taken.
''' '''
server_logger.info('Start logtransfer service') log_print('Start logtransfer service')
logstash_logger, rotating_logfile_handler = _initialize_logger_and_handler() logstash_logger, rotating_logfile_handler = _initialize_logger_and_handler()
......
...@@ -9,6 +9,8 @@ import logging ...@@ -9,6 +9,8 @@ import logging
from nomad.utils.structlogging import LogstashFormatter from nomad.utils.structlogging import LogstashFormatter
from nomad import config from nomad import config
logtransfer.enable_log_print = False
def standard_test_log_record(msg='testmsg') -> bytes: def standard_test_log_record(msg='testmsg') -> bytes:
record = LogRecord(name='test', msg=msg, args=(), exc_info=None, lineno=1, level=logging.INFO, record = LogRecord(name='test', msg=msg, args=(), exc_info=None, lineno=1, level=logging.INFO,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment