-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ticket 23 configure bluesky logging #40
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2682a39
Added logger
7c0e478
Logging changes
9c1f6c9
Addded unit tests
b378003
Added tests for the handler
c5f609b
Removed commented codes
59d1011
Documentation for logger
6cae0a2
Merge branch 'main' into Ticket_23_Configure_Bluesky_Logging
Chsudeepta 9724796
Fixes for lint issues
cd33ec5
Ruff reformatting done
3a39643
added asssert to check type
e91dbd9
added asssert to check type
c7c1342
added asssert to check type
b6f0e9a
Ruff reformatting
e539829
Satiate pyright
1da8554
Satiate pyright
47a1c86
satisfying pyright
5ea54f7
Reformatted
4697ae1
Added comments for changing the log level
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Bluesky logger | ||
|
||
To invoke the bluesky logger, import as `from ibex_bluesky_core.logger import logger` and use it at the desired level: | ||
`logger.blueskylogger.warning("Message to be logged")` | ||
The logger utilizes a `TimedRotatingFileHandler` defined in the `logging.conf` file that rolls over the log at midnight. | ||
|
||
The default logging level is defined at `INFO`. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Bluesky specific logging utility.""" | ||
|
||
import logging | ||
import logging.config | ||
import os | ||
from pathlib import Path | ||
|
||
"""Logger for bluesky. Loads the configured log handler and also attaches to default bluesky logger | ||
To use me | ||
1. you need to import me --> from ibex_bluesky_core.logger import logger | ||
2. Use me at the level you want --> logger.blueskylogger.info("Some useful message") | ||
3. To change the log level check the logging.conf file""" | ||
|
||
BLUESKY_LOGGER = "bluesky" | ||
DEFAULT_LOGGER = "blueskylogs.log" | ||
LOG_FOLDER = os.path.join("C:\\", "instrument", "var", "logs", BLUESKY_LOGGER) | ||
|
||
# Find the log directory, if already set in the environment, else use the default | ||
log_location = os.environ.get("BLUESKY_LOGS", LOG_FOLDER) | ||
# Create the log directory if it doesn't already exist | ||
os.makedirs(log_location, exist_ok=True) | ||
|
||
filepath = Path(__file__).resolve().parent | ||
# disable_existing_loggers ensures all loggers have same configuration as below | ||
logging.config.fileConfig(os.path.join(filepath, "logging.conf"), disable_existing_loggers=False) | ||
|
||
blueskylogger = logging.getLogger("blueskycore") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[loggers] | ||
keys=root,blueskycore | ||
|
||
[handlers] | ||
keys=timedRotatingFileHandler | ||
|
||
[formatters] | ||
keys=simpleFormatter,debugFormatter | ||
|
||
[logger_root] | ||
level=INFO | ||
handlers=timedRotatingFileHandler | ||
|
||
[logger_blueskycore] | ||
level=INFO | ||
qualname=blueskycore | ||
propagate=0 | ||
handlers=timedRotatingFileHandler | ||
|
||
[handler_timedRotatingFileHandler] | ||
class=logging.handlers.TimedRotatingFileHandler | ||
level=INFO | ||
formatter=debugFormatter | ||
#args=set the file name to blueskylogs.log and the log folder as set in the environment variable; if not set, set to default - \instrument\var\logs\bluesky | ||
args=(os.path.join(os.environ.get("BLUESKY_LOGS", os.path.join("C:\\", "instrument", "var", "logs", "bluesky")),'blueskylogs.log'), "midnight") | ||
|
||
[formatter_simpleFormatter] | ||
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s | ||
|
||
[formatter_debugFormatter] | ||
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import sys | ||
from logging.handlers import TimedRotatingFileHandler | ||
|
||
from ibex_bluesky_core.logger import logger | ||
|
||
LOG_MESSAGE = "Logging something to " | ||
LOG_FILE_NAME = "blueskylogs.log" | ||
|
||
|
||
def test_GIVEN_logging_is_requested_THEN_handler_is_added(): | ||
this_function_name = sys._getframe().f_code.co_name | ||
message = LOG_MESSAGE + this_function_name | ||
# Log invocation. | ||
logger.blueskylogger.info(message) | ||
|
||
loghandler = None | ||
for handler in logger.blueskylogger.handlers: | ||
if isinstance(handler, TimedRotatingFileHandler): | ||
loghandler = handler | ||
|
||
assert isinstance(loghandler, TimedRotatingFileHandler) | ||
assert loghandler is not None | ||
assert loghandler.name == "timedRotatingFileHandler" | ||
assert loghandler.baseFilename.endswith(LOG_FILE_NAME) | ||
assert loghandler is not None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking a bit here sorry- I think we should provide some more info on how to change the log level as this is one of the acceptance criteria. e.g Where do I find info on what different log levels there are? And where do I make this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logging levels are sort of python standard - DEBUG, INFO, WARNING etc. And we invoke them as --> mylogger.warning("message"), mylogger.error("another message"). I think it might be overkill to describe the python standard logging levels and calls here.
As described here, the log handler is defined in logging.conf and that's where the default logging level is set to INFO for the handler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay cheers thats all fine then, merging now