Skip to content

Commit

Permalink
Get logging correct
Browse files Browse the repository at this point in the history
That took a few iterations to understand how this actually works
  • Loading branch information
gordonwatts committed May 26, 2021
1 parent 2c350d2 commit dab16ec
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ Another pattern in the above code that one might find useful - a thread-safe way
In the end, all DID finders for ServiceX will run under Kubernetes. ServiceX comes with a built in logging mechanism. If anything is to be logged it should use the log system using the python standard `logging` module, with some extra information. For example, here is how to log a message from your callback function:

```python
import logger
__log = logger.getLogger(__name__)
async def my_callback(did_name: str, info: Dict[str, Any]):
info['log'].info(f'Looking up dataset {did_name}.',
extras={'requestId': info['request-id']})
__log.info(f'Looking up dataset {did_name}.',
extra={'requestId': info['request-id']})

for i in range(0, 10):
yield {
'file_path': f"root://atlas-experiment.cern.ch/dataset1/file{i}.root",
Expand All @@ -123,3 +126,7 @@ In the end, all DID finders for ServiceX will run under Kubernetes. ServiceX com
```

Note the parameter `request-id`: this marks the log messages with the request id that triggered this DID request. This will enable the system to track all log messages across all containers connected with this particular request id - making debugging a lot easier.

The `start_did_finder` will configure the python root logger properly to dump messages with a request ID in them.

*NOTE*: Once `start_did_finder` is called, whenever you log a message you _must_ pass the extra argument. If not, a formatting exception will occur during logging and the message will be swallowed.
7 changes: 4 additions & 3 deletions src/servicex_did_finder_lib/communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from make_it_sync import make_sync

from servicex_did_finder_lib.did_summary import DIDSummary
from servicex_did_finder_lib.logging import initialize_logging
from servicex_did_finder_lib.logging import initialize_root_logger
from .servicex_adaptor import ServiceXAdapter

# The type for the callback method to handle DID's, supplied by the user.
Expand All @@ -22,6 +22,7 @@
# is also changed in the ServiceX_App
QUEUE_NAME_POSTFIX = '_did_requests'

# Easy to use local logger
__logging = logging.getLogger(__name__)
__logging.addHandler(logging.NullHandler())

Expand Down Expand Up @@ -82,7 +83,6 @@ def rabbit_mq_callback(user_callback: UserDIDHandler, channel, method, propertie

info = {
'request-id': request_id,
'log': initialize_logging()
}

# Process the request and resolve the DID
Expand Down Expand Up @@ -185,7 +185,8 @@ def start_did_finder(did_finder_name: str,
default_command_line_args(parser)
parsed_args = parser.parse_args()

# Parse the arguments, and get the callback going
# Initialize the root logger
initialize_root_logger(did_finder_name)

# Start up rabbit mq and also callbacks
init_rabbit_mq(callback,
Expand Down
4 changes: 2 additions & 2 deletions src/servicex_did_finder_lib/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def format(self, record: logging.LogRecord) -> str:
return super().format(record)


def initialize_logging():
def initialize_root_logger(did_scheme: str):
"""
Get a logger and initialize it so that it outputs the correct format
:param request: Request id to insert into log messages
Expand All @@ -35,7 +35,7 @@ def initialize_logging():
log = logging.getLogger()
instance = os.environ.get('INSTANCE_NAME', 'Unknown')
formatter = DIDFormatter('%(levelname)s ' +
f"{instance} rucio_did_finder " +
f"{instance} {did_scheme}_did_finder " +
'%(requestId)s %(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
Expand Down

0 comments on commit dab16ec

Please sign in to comment.