-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jacob Knightley
authored and
Jacob Knightley
committed
Jan 3, 2025
1 parent
7539d6c
commit 326ee30
Showing
12 changed files
with
318 additions
and
202 deletions.
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
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
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
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 |
---|---|---|
@@ -1,5 +1,74 @@ | ||
from fabric_cicd.fabric_workspace import FabricWorkspace | ||
""" | ||
Initializes the fabric_cicd package and sets up logging. | ||
""" | ||
|
||
from fabric_cicd.fabric_workspace import FabricWorkspace | ||
from fabric_cicd.publish import publish_all_items, unpublish_all_orphan_items | ||
import logging | ||
import colorlog | ||
|
||
|
||
def _configure_logger(level: int = logging.INFO) -> None: | ||
""" | ||
Configure the logger. | ||
:param level: The log level to set. Must be one of the standard logging levels. | ||
""" | ||
# Configure logging | ||
log_formatter = "%(asctime)s - %(levelname)s - %(name)s - %(message)s" | ||
|
||
logging.basicConfig( | ||
level=( | ||
# For non-fabric_cicd packages: INFO if DEBUG, else ERROR | ||
logging.INFO | ||
if level == logging.DEBUG | ||
else logging.ERROR | ||
), | ||
format=log_formatter, | ||
filename="fabric_cicd.error.log", | ||
filemode="w", | ||
) | ||
|
||
package_logger = logging.getLogger("fabric_cicd") | ||
package_logger.setLevel(level) | ||
package_logger.handlers = [] | ||
|
||
# Configure Console Handler | ||
console_handler = logging.StreamHandler() | ||
console_handler.setLevel(level) | ||
|
||
console_formatter = colorlog.ColoredFormatter( | ||
"%(log_color)s[%(levelname)s] %(asctime)s - %(message)s", | ||
datefmt="%H:%M:%S", | ||
log_colors={ | ||
"DEBUG": "cyan", | ||
"INFO": "green", | ||
"WARNING": "yellow", | ||
"ERROR": "red", | ||
"CRITICAL": "bold_red", | ||
}, | ||
) | ||
console_handler.setFormatter(console_formatter) | ||
|
||
# Add the handler to the logger | ||
package_logger.addHandler(console_handler) | ||
|
||
|
||
# Initial logger configuration | ||
_configure_logger() | ||
|
||
|
||
def enable_debug_log() -> None: | ||
""" | ||
Set the log level for all loggers within the fabric_cicd package to DEBUG. | ||
""" | ||
|
||
_configure_logger(logging.DEBUG) | ||
|
||
|
||
__all__ = ["FabricWorkspace", "publish_all_items", "unpublish_all_orphan_items"] | ||
__all__ = [ | ||
"FabricWorkspace", | ||
"publish_all_items", | ||
"unpublish_all_orphan_items", | ||
"enable_debug_log", | ||
] |
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
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,59 @@ | ||
import json | ||
import logging | ||
|
||
|
||
class ParsingError(Exception): | ||
def __init__(self, message): | ||
super().__init__(message) | ||
|
||
|
||
class InputError(Exception): | ||
def __init__(self, message): | ||
super().__init__(message) | ||
|
||
|
||
class TokenError(Exception): | ||
def __init__(self, message): | ||
super().__init__(message) | ||
|
||
|
||
class InvokeError(Exception): | ||
def __init__(self, message, response, method, url, body, logger): | ||
super().__init__(message) | ||
log_invoke_payload(logger, response, method, url, body, error=True) | ||
|
||
|
||
def log_invoke_payload(logger, response, method, url, body, error=False): | ||
debug_message = [ | ||
f"\nURL: {url}", | ||
f"Method: {method}", | ||
( | ||
f"Request Body:\n{json.dumps(body, indent=4)}" | ||
if body | ||
else "Request Body: None" | ||
), | ||
] | ||
if response is not None: | ||
debug_message.extend( | ||
[ | ||
f"Response Status: {response.status_code}", | ||
"Response Headers:", | ||
json.dumps(dict(response.headers), indent=4), | ||
"Response Body:", | ||
( | ||
json.dumps(response.json(), indent=4) | ||
if response.headers.get("Content-Type") == "application/json" | ||
else response.text | ||
), | ||
"", | ||
] | ||
) | ||
|
||
# Join all parts into a single message | ||
full_debug_message = "\n".join(debug_message) | ||
if error: | ||
logger.error(full_debug_message) | ||
elif logger.isEnabledFor(logging.DEBUG): | ||
logger.debug(full_debug_message) | ||
else: | ||
logger.info(full_debug_message) |
Oops, something went wrong.