colored_logger
colored_logger ¶
Colored logger wrapper that automatically applies colors to log levels.
This module provides a ColoredLogger class that wraps cl and automatically applies appropriate colors to different log levels for better visual distinction in console output.
Quick Start for Users and AI Agents¶
Standard Usage (Recommended):
from crew_dcs.utils.logging import get_colored_logger
# Get a logger with default settings (INFO level, colored output)
logger = get_colored_logger()
# Use in async functions
await logger.info("Operation completed")
await logger.warning("This might be a problem")
await logger.error("Something went wrong")
Configuration for Scripts/Notebooks:
from crew_dcs.utils.logging import set_domolibrary_logger
# Configure logger with custom settings
logger = set_domolibrary_logger(
app_name="my_script",
min_level="DEBUG", # Show DEBUG, INFO, WARNING, ERROR, CRITICAL
set_as_global=True, # Make this the global logger for all modules
)
Adjust Level Dynamically:
# Show more verbose output
logger.set_level("DEBUG")
# Show only warnings and errors
logger.set_level("WARNING")
# Check current level
current_level = logger.get_level() # Returns 'DEBUG', 'INFO', etc.
For Route Functions (API calls):
from crew_dcs.utils.logging import log_call, LogDecoratorConfig
@log_call(
level_name="route",
config=LogDecoratorConfig(
entity_extractor=DomoEntityExtractor(),
result_processor=DomoEntityResultProcessor(),
),
)
async def my_route_function(auth, **kwargs):
# Function automatically logs entry/exit with colored output
pass
Level Hierarchy: - DEBUG (10) - Most verbose, shows everything - INFO (20) - Normal operations (default) - WARNING (30) - Warning messages only - ERROR (40) - Error messages only - CRITICAL (50) - Most severe only
Important Notes:
- ALWAYS import from crew_dcs.utils.logging, NEVER from cl directly
- NEVER import cl in crew_dcs code
- Logger methods are async: use await logger.info(), not logger.info()
- Use set_domolibrary_logger() at script entry points for global configuration
- Use get_colored_logger() within modules for local logger instances
ColoredLogger ¶
ColoredLogger(
base_logger: CrewLogger,
debug_color: str = "cyan",
info_color: str = "green",
warning_color: str = "yellow",
error_color: str = "bold_red",
critical_color: str = "bold_red",
min_level: str = "INFO",
exclude_patterns: list[str] | None = None,
)
Bases: CrewLogger
Logger that automatically colorizes messages by log level.
This class wraps cl's Logger and adds automatic coloring and level filtering. It's the standard logger for all crew_dcs code.
Default colors
- DEBUG: cyan
- INFO: green
- WARNING: yellow
- ERROR: red
- CRITICAL: bold_red
Usage
from crew_dcs.utils.logging import get_colored_logger
Basic usage¶
logger = get_colored_logger() await logger.info("This will be green") await logger.warning("This will be yellow") await logger.error("This will be red")
Control verbosity¶
logger.set_level("DEBUG") # Show all messages logger.set_level("WARNING") # Show only warnings and errors
Check current level¶
print(f"Current level: {logger.get_level()}")
Note
- All log methods are ASYNC - must use
await - Level filtering happens at this layer before passing to cl
- Use
set_domolibrary_logger()for global configuration - Use
get_colored_logger()for module-level instances
Initialize colored logger wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_logger
|
CrewLogger
|
The underlying cl Logger instance |
required |
debug_color
|
str
|
Color for debug messages (default: cyan) |
'cyan'
|
info_color
|
str
|
Color for info messages (default: green) |
'green'
|
warning_color
|
str
|
Color for warning messages (default: yellow) |
'yellow'
|
error_color
|
str
|
Color for error messages (default: bold_red) |
'bold_red'
|
critical_color
|
str
|
Color for critical messages (default: bold_red) |
'bold_red'
|
min_level
|
str
|
Minimum log level to display (default: INFO) |
'INFO'
|
exclude_patterns
|
list[str] | None
|
List of message patterns to filter out (default: None) |
None
|
Source code in src/crew_dcs/utils/logging/colored_logger.py
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
critical
async
¶
critical(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any,
) -> bool
Log CRITICAL level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 | |
critical_sync ¶
critical_sync(
message: str, color: str | None = None, **context: Any
) -> None
Sync CRITICAL log.
Source code in src/crew_dcs/utils/logging/colored_logger.py
823 824 825 826 827 | |
debug
async
¶
debug(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any,
) -> bool
Log DEBUG level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 | |
debug_sync ¶
debug_sync(
message: str, color: str | None = None, **context: Any
) -> None
Sync DEBUG log.
Source code in src/crew_dcs/utils/logging/colored_logger.py
791 792 793 794 795 | |
error
async
¶
error(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any,
) -> bool
Log ERROR level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 | |
error_sync ¶
error_sync(
message: str, color: str | None = None, **context: Any
) -> None
Sync ERROR log.
Source code in src/crew_dcs/utils/logging/colored_logger.py
807 808 809 810 811 | |
exception_sync ¶
exception_sync(
message: str, color: str | None = None, **context: Any
) -> None
Sync ERROR log that records the active exception (call from an except block).
Source code in src/crew_dcs/utils/logging/colored_logger.py
813 814 815 816 817 818 819 820 821 | |
get_level ¶
get_level() -> str
Get the current minimum log level.
Returns:
| Type | Description |
|---|---|
str
|
Current log level name (e.g., 'INFO', 'WARNING', 'ERROR') |
Example
logger = get_colored_logger() logger.get_level() 'INFO'
Source code in src/crew_dcs/utils/logging/colored_logger.py
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | |
info
async
¶
info(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any,
) -> bool
Log INFO level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 | |
info_sync ¶
info_sync(
message: str, color: str | None = None, **context: Any
) -> None
Sync INFO log.
Source code in src/crew_dcs/utils/logging/colored_logger.py
797 798 799 | |
log
async
¶
log(level: Any, message: str, **context: Any) -> bool
Log with automatic coloring for cl log() usage.
Source code in src/crew_dcs/utils/logging/colored_logger.py
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | |
log_sync ¶
log_sync(level: Any, message: str, **context: Any) -> None
Sync counterpart of log.
Source code in src/crew_dcs/utils/logging/colored_logger.py
829 830 831 832 833 834 835 836 837 838 839 840 841 | |
set_level ¶
set_level(level: str)
Set the minimum log level for this logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Log level name - 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL' |
required |
Example
logger = get_colored_logger() logger.set_level('WARNING') # Only show WARNING and above logger.set_level('ERROR') # Only show ERROR and CRITICAL logger.set_level('INFO') # Show INFO and above (default)
Source code in src/crew_dcs/utils/logging/colored_logger.py
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | |
warning
async
¶
warning(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any,
) -> bool
Log WARNING level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 | |
warning_sync ¶
warning_sync(
message: str, color: str | None = None, **context: Any
) -> None
Sync WARNING log.
Source code in src/crew_dcs/utils/logging/colored_logger.py
801 802 803 804 805 | |
LogDecoratorConfig
dataclass
¶
LogDecoratorConfig(
entity_extractor: Any = None,
result_processor: Any = None,
)
Compatibility shim for LogDecoratorConfig.
LogStreamConfigError ¶
LogStreamConfigError(target: str, reason: str)
Bases: ValueError
CREW_DCS_LOG_STREAM names a destination that cannot be written.
Source code in src/crew_dcs/utils/logging/colored_logger.py
108 109 110 111 112 | |
colorize ¶
colorize(message: str, color: str | None = None) -> str
Apply ANSI color codes to a message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to colorize |
required |
color
|
str | None
|
Color name (e.g., 'red', 'green', 'cyan', 'bold_red') |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Colorized message string |
Source code in src/crew_dcs/utils/logging/colored_logger.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
flush_logs
async
¶
flush_logs() -> None
Write out every buffered log record now.
cl buffers records (a batch of 100, or a 30 s timer), so a short script
that ends right after a route call exits before anything is printed, and
debug_api=True output never appears. async with on a DomoAuth
calls this on exit; call it yourself at the end of a script that does not
use async with::
set_domolibrary_logger(min_level="DEBUG")
...
await flush_logs()
Flushes the global logger and the logger under each live module-level
ColoredLogger (they can differ when the global was replaced after import).
Source code in src/crew_dcs/utils/logging/colored_logger.py
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 | |
get_colored_logger ¶
get_colored_logger(
app_name: str | None = None,
debug_color: str = "cyan",
info_color: str = "green",
warning_color: str = "yellow",
error_color: str = "bold_red",
critical_color: str = "bold_red",
env: str = "development",
exclude_patterns: list[str] | None = None,
min_level: str | None = None,
enable_datadog: bool | None = None,
set_as_global: bool = False,
force: bool = False,
) -> ColoredLogger
Get or create a colored logger instance with advanced features.
Standard Usage (Module-Level): Call with no parameters to get the existing global logger. This is the recommended pattern for module-level logger variables: >>> logger = get_colored_logger() # Gets existing global logger
Advanced Usage (Script Entry Points): Pass app_name to create a new logger with custom handlers. Use set_as_global=True to make it the global logger. For most cases, prefer set_domolibrary_logger() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name
|
str | None
|
Name of the application for logging context (default: None) If provided, creates a NEW logger with handlers. If None, returns existing global logger. |
None
|
debug_color
|
str
|
Color for debug messages (default: cyan) |
'cyan'
|
info_color
|
str
|
Color for info messages (default: green) |
'green'
|
warning_color
|
str
|
Color for warning messages (default: yellow) |
'yellow'
|
error_color
|
str
|
Color for error messages (default: bold_red) |
'bold_red'
|
critical_color
|
str
|
Color for critical messages (default: bold_red) |
'bold_red'
|
env
|
str
|
Environment name ('development', 'production', 'staging', etc.) |
'development'
|
exclude_patterns
|
list[str] | None
|
List of message patterns to filter out (default: None) |
None
|
min_level
|
str | None
|
Minimum log level to display (default: inherit from global logger or INFO) |
None
|
enable_datadog
|
bool | None
|
Enable Datadog handler (auto-detect from env if None) |
None
|
set_as_global
|
bool
|
Set this as cl's global logger (default: False) Only set to True when configuring at entry points. Modules should leave as False. |
False
|
force
|
bool
|
Force setting as global even if user has configured logger (default: False) |
False
|
Returns:
| Type | Description |
|---|---|
ColoredLogger
|
ColoredLogger instance with automatic color application |
Example
Standard usage - get existing global logger (recommended for modules)¶
logger = get_colored_logger() await logger.info("Success!") # Will be green
Advanced usage - create new logger at entry point¶
logger = get_colored_logger( ... app_name="MyApp", ... min_level="DEBUG", ... set_as_global=True, # Set as global for all modules ... )
For entry points, prefer set_domolibrary_logger() instead:¶
from crew_dcs.utils.logging import set_domolibrary_logger logger = set_domolibrary_logger(app_name="MyApp", min_level="DEBUG")
Source code in src/crew_dcs/utils/logging/colored_logger.py
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 | |
log_call ¶
log_call(
action_name: str | None = None,
level_name: str | None = None,
config: Any = None,
color: str | None = None,
logger: CrewLogger | None = None,
log_level: str | None = None,
**kwargs: Any,
) -> Callable
Compatibility wrapper for log_call decorator.
Maps old API parameters to new log_function_call API. Also filters out 'logger' parameter that log_function_call injects.
Note: Route functions (level_name='route') default to DEBUG level for API operations. Business logic decisions should use INFO level explicitly in method implementations.
Source code in src/crew_dcs/utils/logging/colored_logger.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
set_domolibrary_logger ¶
set_domolibrary_logger(
logger: ColoredLogger | None = None,
set_as_global: bool = True,
**kwargs,
) -> ColoredLogger
Explicitly set the domolibrary logger, optionally making it the global logger.
This is the recommended way to configure logging for domolibrary if you want colored output throughout the library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
ColoredLogger | None
|
Existing ColoredLogger instance, or None to create a new one |
None
|
set_as_global
|
bool
|
Set this as cl's global logger (default: True) |
True
|
**kwargs
|
Arguments to pass to get_colored_logger if creating a new instance |
{}
|
Returns:
| Type | Description |
|---|---|
ColoredLogger
|
The ColoredLogger instance |
Example
Option 1: Use default colored logger¶
from crew_dcs.utils.logging import set_domolibrary_logger logger = set_domolibrary_logger()
Option 2: Create with custom colors¶
logger = set_domolibrary_logger( ... info_color="blue", ... error_color="magenta" ... )
Option 3: Use your own ColoredLogger¶
my_logger = ColoredLogger(base_logger=my_base_logger) set_domolibrary_logger(logger=my_logger)
Source code in src/crew_dcs/utils/logging/colored_logger.py
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 | |