|
32 | 32 | from typing import NamedTuple |
33 | 33 | from typing import TYPE_CHECKING |
34 | 34 |
|
| 35 | +from clusterfuzz._internal.system import environment |
| 36 | + |
35 | 37 | # This is needed to avoid circular import |
36 | 38 | if TYPE_CHECKING: |
37 | 39 | from clusterfuzz._internal.cron.grouper import TestcaseAttributes |
@@ -93,26 +95,17 @@ def _console_logging_enabled(): |
93 | 95 |
|
94 | 96 | # TODO(pmeuleman) Revert the changeset that added these once |
95 | 97 | # https://github.com/google/clusterfuzz/pull/3422 lands. |
96 | | -def _file_logging_enabled(): |
| 98 | +def _file_logging_enabled() -> bool: |
97 | 99 | """Return bool True when logging to files (bot/logs/*.log) is enabled. |
98 | | - This is enabled by default. |
99 | | - This is disabled if we are running in app engine or kubernetes as these have |
100 | | - their dedicated loggers, see configure_appengine() and configure_k8s(). |
101 | | - """ |
102 | | - return bool(os.getenv( |
103 | | - 'LOG_TO_FILE', |
104 | | - 'True')) and not _is_running_on_app_engine() and not _is_running_on_k8s() |
| 100 | + This is enabled by default.""" |
| 101 | + return environment.get_value('LOG_TO_FILE', True) |
105 | 102 |
|
106 | 103 |
|
107 | | -def _cloud_logging_enabled(): |
| 104 | +def _cloud_logging_enabled() -> bool: |
108 | 105 | """Return bool True where Google Cloud Logging is enabled. |
109 | | - This is enabled by default. |
110 | | - This is disabled for local development and if we are running in a app engine |
111 | | - or kubernetes as these have their dedicated loggers, see |
112 | | - configure_appengine() and configure_k8s().""" |
113 | | - return (bool(os.getenv('LOG_TO_GCP', 'True')) and |
114 | | - not os.getenv("PY_UNITTESTS") and not _is_local() and |
115 | | - not _is_running_on_app_engine() and not _is_running_on_k8s()) |
| 106 | + This is enabled by default but disabled for local development.""" |
| 107 | + return (environment.get_value('LOG_TO_GCP', True) and |
| 108 | + not environment.is_running_unit_tests() and not _is_local()) |
116 | 109 |
|
117 | 110 |
|
118 | 111 | def suppress_unwanted_warnings(): |
@@ -418,7 +411,7 @@ def configure_appengine(): |
418 | 411 | """Configure logging for App Engine.""" |
419 | 412 | logging.getLogger().setLevel(logging.INFO) |
420 | 413 |
|
421 | | - if os.getenv('LOCAL_DEVELOPMENT') or os.getenv('PY_UNITTESTS'): |
| 414 | + if os.getenv('LOCAL_DEVELOPMENT') or environment.is_running_unit_tests(): |
422 | 415 | return |
423 | 416 |
|
424 | 417 | import google.cloud.logging |
@@ -554,12 +547,38 @@ def cloud_label_filter(record): |
554 | 547 | logging.getLogger().addHandler(handler) |
555 | 548 |
|
556 | 549 |
|
| 550 | +def configure_swarming(name: str, extras: dict[str, str] | None = None) -> None: |
| 551 | + """Configure logging for swarming bots.""" |
| 552 | + if extras is None: |
| 553 | + extras = {} |
| 554 | + extras['task_id'] = os.getenv('TASK_ID') |
| 555 | + extras['instance_id'] = os.getenv('BOT_NAME') |
| 556 | + extras['platform'] = 'swarming' |
| 557 | + |
| 558 | + global _default_extras |
| 559 | + _default_extras = extras |
| 560 | + |
| 561 | + logging.basicConfig(level=logging.INFO) |
| 562 | + if _cloud_logging_enabled(): |
| 563 | + configure_cloud_logging() |
| 564 | + |
| 565 | + logger = logging.getLogger(name) |
| 566 | + logger.setLevel(logging.INFO) |
| 567 | + set_logger(logger) |
| 568 | + |
| 569 | + sys.excepthook = uncaught_exception_handler |
| 570 | + |
| 571 | + |
557 | 572 | def configure(name, extras=None): |
558 | 573 | """Set logger. See the list of loggers in bot/config/logging.yaml. |
559 | 574 | Also configures the process to log any uncaught exceptions as an error. |
560 | 575 | |extras| will be included by emit() in log messages.""" |
561 | 576 | suppress_unwanted_warnings() |
562 | 577 |
|
| 578 | + if environment.is_running_on_swarming(): |
| 579 | + configure_swarming(name, extras) |
| 580 | + return |
| 581 | + |
563 | 582 | if _is_running_on_k8s(): |
564 | 583 | configure_k8s() |
565 | 584 | return |
@@ -792,7 +811,6 @@ def get_common_log_context() -> dict[str, str]: |
792 | 811 | """Return common context to be propagated by logs.""" |
793 | 812 | # Avoid circular imports on the top level. |
794 | 813 | from clusterfuzz._internal.base import utils |
795 | | - from clusterfuzz._internal.system import environment |
796 | 814 |
|
797 | 815 | try: |
798 | 816 | os_type = environment.platform() |
|
0 commit comments