Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Starts an application.
5"""
6from lightmlrestapi.mlapp.base_logging import BaseLogging # pylint: disable=C0411
9class LogApp(BaseLogging):
10 """
11 Defines methods to easily log information for a web application.
12 The function *fct_session* returns information
13 about the session whatever it is as a dictionary.
14 """
16 def __init__(self, folder='.', secret_log=None, fct_session=None, **kwargs):
17 """
18 @param fct_session function to return information about a session
19 @param secret_log to encrypt log (None to ignore)
20 @param folder folder where to write the logs (None to disable the logging)
21 @param kwargs additional parameters for :epkg:`BaseLogging`
22 """
23 BaseLogging.__init__(self, secret=secret_log, folder=folder, **kwargs)
24 self.get_session = fct_session
26 def log_any(self, tag, msg, request, session=None, **data):
27 """
28 Logs information.
30 @param tag tag (to filter rows in logs)
31 @param msg event kind
32 @param request request
33 @param session information about the session
34 @param data addition data
35 """
36 if not session and self.get_session:
37 session = self.get_session(request)
38 client = request["client"]
39 log = dict(msg=msg, session=session, client=client)
40 log.update(data)
41 self.info(tag, log)
43 def log_event(self, msg, request, session=None, **data):
44 """
45 Logs information about events.
47 @param msg event kind
48 @param request request
49 @param session information about the session
50 @param data addition data
51 """
52 if not session and self.get_session:
53 session = self.get_session(request)
54 self.log_any('[DATA]', msg, request, session=session, **data)