Skip to content

Commit 6dc1bf6

Browse files
Merge pull request #298 from sebulibah/micro_services.custom_logging-logger
Improve logging - satosa.micro_services.custom_logging
2 parents 5805645 + 0baecc3 commit 6dc1bf6

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/satosa/micro_services/custom_logging.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
SATOSA microservice that outputs log in custom format.
33
"""
44

5-
from .base import ResponseMicroService
6-
from satosa.logging_util import satosa_logging
7-
from base64 import urlsafe_b64encode, urlsafe_b64decode
8-
9-
import json
105
import copy
6+
import json
117
import logging
128

9+
import satosa.logging_util as lu
10+
from .base import ResponseMicroService
11+
12+
1313
logger = logging.getLogger(__name__)
1414

15+
1516
class CustomLoggingService(ResponseMicroService):
1617
"""
1718
Use context and data object to create custom log output
@@ -21,7 +22,7 @@ class CustomLoggingService(ResponseMicroService):
2122
def __init__(self, config, *args, **kwargs):
2223
super().__init__(*args, **kwargs)
2324
self.config = config
24-
25+
2526
def process(self, context, data):
2627
logprefix = CustomLoggingService.logprefix
2728

@@ -30,18 +31,26 @@ def process(self, context, data):
3031
config = self.config
3132
configClean = copy.deepcopy(config)
3233

33-
satosa_logging(logger, logging.DEBUG, "{} Using default configuration {}".format(logprefix, configClean), context.state)
34+
msg = "{} Using default configuration {}".format(logprefix, configClean)
35+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
36+
logger.debug(logline)
3437

3538
# Find the entityID for the SP that initiated the flow and target IdP
3639
try:
3740
spEntityID = context.state.state_dict['SATOSA_BASE']['requester']
3841
idpEntityID = data.auth_info.issuer
3942
except KeyError as err:
40-
satosa_logging(logger, logging.ERROR, "{} Unable to determine the entityID's for the IdP or SP".format(logprefix), context.state)
43+
msg = "{} Unable to determine the entityID's for the IdP or SP".format(logprefix)
44+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
45+
logger.error(logline)
4146
return super().process(context, data)
4247

43-
satosa_logging(logger, logging.DEBUG, "{} entityID for the SP requester is {}".format(logprefix, spEntityID), context.state)
44-
satosa_logging(logger, logging.ERROR, "{} entityID for the target IdP is {}".format(logprefix, idpEntityID), context.state)
48+
msg = "{} entityID for the SP requester is {}".format(logprefix, spEntityID)
49+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
50+
logger.debug(logline)
51+
msg = "{} entityID for the target IdP is {}".format(logprefix, idpEntityID)
52+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
53+
logger.error(logline)
4554

4655
# Obtain configuration details from the per-SP configuration or the default configuration
4756
try:
@@ -57,17 +66,25 @@ def process(self, context, data):
5766

5867

5968
except KeyError as err:
60-
satosa_logging(logger, logging.ERROR, "{} Configuration '{}' is missing".format(logprefix, err), context.state)
69+
msg = "{} Configuration '{}' is missing".format(logprefix, err)
70+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
71+
logger.error(logline)
6172
return super().process(context, data)
6273

6374
record = None
6475

6576
try:
66-
satosa_logging(logger, logging.DEBUG, "{} Using context {}".format(logprefix, context), context.state)
67-
satosa_logging(logger, logging.DEBUG, "{} Using data {}".format(logprefix, data.to_dict()), context.state)
77+
msg = "{} Using context {}".format(logprefix, context)
78+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
79+
logger.debug(logline)
80+
msg = "{} Using data {}".format(logprefix, data.to_dict())
81+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
82+
logger.debug(logline)
6883

6984
# Open log_target file
70-
satosa_logging(logger, logging.DEBUG, "{} Opening log_target file {}".format(logprefix, log_target), context.state)
85+
msg = "{} Opening log_target file {}".format(logprefix, log_target)
86+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
87+
logger.debug(logline)
7188
loghandle = open(log_target,"a")
7289

7390
# This is where the logging magic happens
@@ -78,15 +95,19 @@ def process(self, context, data):
7895
log['idp'] = idpEntityID
7996
log['sp'] = spEntityID
8097
log['attr'] = { key: data.to_dict()['attr'].get(key) for key in attrs }
81-
98+
8299
print(json.dumps(log), file=loghandle, end="\n")
83100

84101
except Exception as err:
85-
satosa_logging(logger, logging.ERROR, "{} Caught exception: {0}".format(logprefix, err), None)
102+
msg = "{} Caught exception: {}".format(logprefix, err)
103+
logline = lu.LOG_FMT.format(id=lu.get_session_id(None), message=msg)
104+
logger.error(logline)
86105
return super().process(context, data)
87106

88107
else:
89-
satosa_logging(logger, logging.DEBUG, "{} Closing log_target file".format(logprefix), context.state)
108+
msg = "{} Closing log_target file".format(logprefix)
109+
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
110+
logger.debug(logline)
90111

91112
# Close log_target file
92113
loghandle.close()

0 commit comments

Comments
 (0)