Skip to content

Commit

Permalink
Remove duplicate logging section
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed-dash committed Nov 24, 2023
1 parent 9dabbf7 commit 1e86108
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 196 deletions.
100 changes: 2 additions & 98 deletions src/AoC_2017/Dazbo's_Advent_of_Code_2017.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -313,102 +313,6 @@
"Now we load a bunch of helper functions and classes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SwzjfUFCKhXe",
"tags": []
},
"outputs": [],
"source": [
"##########################################################################\n",
"# SETUP LOGGING\n",
"#\n",
"# Create a new instance of \"logger\" in the client application\n",
"# Set to your preferred logging level\n",
"# And add the stream_handler from this module, if you want coloured output\n",
"##########################################################################\n",
"\n",
"# logger for aoc_commons only\n",
"logger = logging.getLogger(__name__) # aoc_common.aoc_commons\n",
"logger.setLevel(logging.INFO)\n",
"stream_handler = None\n",
"\n",
"class ColouredFormatter(logging.Formatter):\n",
" \"\"\" Custom Formater which adds colour to output, based on logging level \"\"\"\n",
"\n",
" level_mapping = {\"DEBUG\": (Fore.BLUE, \"DBG\"),\n",
" \"INFO\": (Fore.GREEN, \"INF\"),\n",
" \"WARNING\": (Fore.YELLOW, \"WRN\"),\n",
" \"ERROR\": (Fore.RED, \"ERR\"),\n",
" \"CRITICAL\": (Fore.MAGENTA, \"CRT\")\n",
" }\n",
"\n",
" def __init__(self, *args, apply_colour=True, shorten_lvl=True, **kwargs) -> None:\n",
" \"\"\" Args:\n",
" apply_colour (bool, optional): Apply colouring to messages. Defaults to True.\n",
" shorten_lvl (bool, optional): Shorten level names to 3 chars. Defaults to True.\n",
" \"\"\"\n",
" super().__init__(*args, **kwargs)\n",
" self._apply_colour = apply_colour\n",
" self._shorten_lvl = shorten_lvl\n",
"\n",
" def format(self, record):\n",
" if record.levelname in ColouredFormatter.level_mapping:\n",
" new_rec = copy.copy(record)\n",
" colour, new_level = ColouredFormatter.level_mapping[record.levelname]\n",
"\n",
" if self._shorten_lvl:\n",
" new_rec.levelname = new_level\n",
"\n",
" if self._apply_colour:\n",
" msg = colour + super().format(new_rec) + Fore.RESET\n",
" else:\n",
" msg = super().format(new_rec)\n",
"\n",
" return msg\n",
"\n",
" # If our logging message is not using one of these levels...\n",
" return super().format(record)\n",
"\n",
"if not stream_handler:\n",
" stream_handler = logging.StreamHandler()\n",
" stream_fmt = ColouredFormatter(fmt='%(asctime)s.%(msecs)03d:%(name)s - %(levelname)s: %(message)s',\n",
" datefmt='%H:%M:%S')\n",
" stream_handler.setFormatter(stream_fmt)\n",
" \n",
"if not logger.handlers:\n",
" # Add our ColouredFormatter as the default console logging\n",
" logger.addHandler(stream_handler)\n",
"\n",
"def retrieve_console_logger(script_name):\n",
" \"\"\" Create and return a new logger, named after the script\n",
" So, in your calling code, add a line like this:\n",
" logger = ac.retrieve_console_logger(locations.script_name)\n",
" \"\"\"\n",
" a_logger = logging.getLogger(script_name)\n",
" a_logger.addHandler(stream_handler)\n",
" a_logger.propagate = False\n",
" return a_logger\n",
"\n",
"def setup_file_logging(a_logger: logging.Logger, folder: str|Path=\"\"):\n",
" \"\"\" Add a FileHandler to the specified logger. File name is based on the logger name.\n",
" In calling code, we can add a line like this:\n",
" td.setup_file_logging(logger, locations.output_dir)\n",
"\n",
" Args:\n",
" a_logger (Logger): The existing logger\n",
" folder (str): Where the log file will be created. Will be created if it doesn't exist\n",
" \"\"\"\n",
" Path(folder).mkdir(parents=True, exist_ok=True) # Create directory if it does not exist\n",
" file_handler = logging.FileHandler(Path(folder, a_logger.name + \".log\"), mode='w')\n",
" file_fmt = logging.Formatter(fmt=\"%(asctime)s.%(msecs)03d:%(name)s:%(levelname)8s: %(message)s\",\n",
" datefmt='%H:%M:%S')\n",
" file_handler.setFormatter(file_fmt)\n",
" a_logger.addHandler(file_handler)"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -3534,9 +3438,9 @@
"toc_visible": true
},
"kernelspec": {
"display_name": "aca_aoc",
"display_name": "ana-aoc",
"language": "python",
"name": "aca_aoc"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down
100 changes: 2 additions & 98 deletions src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -313,102 +313,6 @@
"Now we load a bunch of helper functions and classes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SwzjfUFCKhXe",
"tags": []
},
"outputs": [],
"source": [
"##########################################################################\n",
"# SETUP LOGGING\n",
"#\n",
"# Create a new instance of \"logger\" in the client application\n",
"# Set to your preferred logging level\n",
"# And add the stream_handler from this module, if you want coloured output\n",
"##########################################################################\n",
"\n",
"# logger for aoc_commons only\n",
"logger = logging.getLogger(__name__) # aoc_common.aoc_commons\n",
"logger.setLevel(logging.INFO)\n",
"stream_handler = None\n",
"\n",
"class ColouredFormatter(logging.Formatter):\n",
" \"\"\" Custom Formater which adds colour to output, based on logging level \"\"\"\n",
"\n",
" level_mapping = {\"DEBUG\": (Fore.BLUE, \"DBG\"),\n",
" \"INFO\": (Fore.GREEN, \"INF\"),\n",
" \"WARNING\": (Fore.YELLOW, \"WRN\"),\n",
" \"ERROR\": (Fore.RED, \"ERR\"),\n",
" \"CRITICAL\": (Fore.MAGENTA, \"CRT\")\n",
" }\n",
"\n",
" def __init__(self, *args, apply_colour=True, shorten_lvl=True, **kwargs) -> None:\n",
" \"\"\" Args:\n",
" apply_colour (bool, optional): Apply colouring to messages. Defaults to True.\n",
" shorten_lvl (bool, optional): Shorten level names to 3 chars. Defaults to True.\n",
" \"\"\"\n",
" super().__init__(*args, **kwargs)\n",
" self._apply_colour = apply_colour\n",
" self._shorten_lvl = shorten_lvl\n",
"\n",
" def format(self, record):\n",
" if record.levelname in ColouredFormatter.level_mapping:\n",
" new_rec = copy.copy(record)\n",
" colour, new_level = ColouredFormatter.level_mapping[record.levelname]\n",
"\n",
" if self._shorten_lvl:\n",
" new_rec.levelname = new_level\n",
"\n",
" if self._apply_colour:\n",
" msg = colour + super().format(new_rec) + Fore.RESET\n",
" else:\n",
" msg = super().format(new_rec)\n",
"\n",
" return msg\n",
"\n",
" # If our logging message is not using one of these levels...\n",
" return super().format(record)\n",
"\n",
"if not stream_handler:\n",
" stream_handler = logging.StreamHandler()\n",
" stream_fmt = ColouredFormatter(fmt='%(asctime)s.%(msecs)03d:%(name)s - %(levelname)s: %(message)s',\n",
" datefmt='%H:%M:%S')\n",
" stream_handler.setFormatter(stream_fmt)\n",
" \n",
"if not logger.handlers:\n",
" # Add our ColouredFormatter as the default console logging\n",
" logger.addHandler(stream_handler)\n",
"\n",
"def retrieve_console_logger(script_name):\n",
" \"\"\" Create and return a new logger, named after the script\n",
" So, in your calling code, add a line like this:\n",
" logger = ac.retrieve_console_logger(locations.script_name)\n",
" \"\"\"\n",
" a_logger = logging.getLogger(script_name)\n",
" a_logger.addHandler(stream_handler)\n",
" a_logger.propagate = False\n",
" return a_logger\n",
"\n",
"def setup_file_logging(a_logger: logging.Logger, folder: str|Path=\"\"):\n",
" \"\"\" Add a FileHandler to the specified logger. File name is based on the logger name.\n",
" In calling code, we can add a line like this:\n",
" td.setup_file_logging(logger, locations.output_dir)\n",
"\n",
" Args:\n",
" a_logger (Logger): The existing logger\n",
" folder (str): Where the log file will be created. Will be created if it doesn't exist\n",
" \"\"\"\n",
" Path(folder).mkdir(parents=True, exist_ok=True) # Create directory if it does not exist\n",
" file_handler = logging.FileHandler(Path(folder, a_logger.name + \".log\"), mode='w')\n",
" file_fmt = logging.Formatter(fmt=\"%(asctime)s.%(msecs)03d:%(name)s:%(levelname)8s: %(message)s\",\n",
" datefmt='%H:%M:%S')\n",
" file_handler.setFormatter(file_fmt)\n",
" a_logger.addHandler(file_handler)"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -1159,9 +1063,9 @@
"toc_visible": true
},
"kernelspec": {
"display_name": "aca_aoc",
"display_name": "ana-aoc",
"language": "python",
"name": "aca_aoc"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down

0 comments on commit 1e86108

Please sign in to comment.