Skip to content

Commit 7304eb0

Browse files
committed
Update logging setup and add tests
1 parent 25a76c8 commit 7304eb0

6 files changed

Lines changed: 59 additions & 5 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ repos:
6767
hooks:
6868
- id: mypy
6969
name: mypy
70-
entry: "python"
70+
entry: "python3"
7171
language: system
7272
types: [python]
7373
args: ["-m", "mypy", "--config-file", "mypy.ini"]

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
# v26.18.0
4+
5+
- Disable logging of initialisation events by default, can be enabled by setting env var `OTG_LOG_INIT_EVENTS=1`
6+
- Ensure that setting `OTF_LOG_LEVEL` to `DEBUG` actually sets the log level to `DEBUG` in the root logger, which ensures addons like the AWS addon will trigger `botocore` to log it's debug messages too.
7+
- Update `README.md` and help text to include new environment variables, and any missing ones.
8+
39
# v26.15.0
410

511
- Add `OTF_STALE_RUNNING_LOG_SECONDS` environment variable to allow resuming of batches from a `_running` log file that is older than the specified number of seconds.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ options:
172172
These are some environment variables that can be used to customise the behaviour of the application. There are some internally used variables too, but changing them without a full understanding of the code is not advised.
173173

174174
- `OTF_NO_LOG` - Disable logging to file. Only log to stderr. Set to `1` to enable
175+
- `OTF_LOG_LEVEL` - Set the log level.
176+
- `OTF_LOG_INIT_EVENTS` - Set to `1` to enable logging of log initialisation events. Defaults to `0`
175177
- `OTF_LOG_JSON` - Stderr logging will be in JSON format. Set to `1` to enable
176178
- `OTF_LOG_DIRECTORY` - Path under which log files are written
177179
- `OTF_NO_THREAD_SLEEP` - Disable the 1-second sleep between batch task thread creation. This sleep exists to prevent race conditions with concurrent protocol imports. Only disable this if you understand the implications. Set to `1` to disable.

src/opentaskpy/cli/task_run.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ def main() -> None:
2828
OTF_NO_LOG - Prevent logging to any files, will log to stdout/err only
2929
OTF_LOG_DIRECTORY - Specify a particular log directory to write log files to
3030
OTF_LOG_LEVEL - Equivalent to using -v
31+
OTF_LOG_INIT_EVENTS - Enable logging of log initialisation events. Set to 1 to enable
32+
OTF_NO_THREAD_SLEEP - Disable the 1-second sleep between batch task thread creation. Set to 1 to disable
3133
OTF_SSH_KEY - Specify a particular SSH key to use for SSH/SFTP related transfers
34+
OTF_STAGING_DIR - Staging base directory to place files before final location. Default is /tmp
35+
OTF_BATCH_RESUME_LOG_DATE - Resume batch runs from a specific date in YYYYMMDD format
3236
OTF_VARIABLES_FILE - Override the default variables file location
37+
OTF_PARAMIKO_ULTRA_DEBUG - Enable Paramiko ultra_debug for verbose SSH communications. Set to 1 to enable
38+
OTF_LAZY_LOAD_VARIABLES - Only load variables used by the task definition. Set to 1 to enable
39+
OTF_STALE_RUNNING_LOG_SECONDS - Resume using stale _running logs after this many inactive seconds
3340
OTF_NOOP - Equivalent to using --noop argument
3441
3542
Task Definition Overrides:

src/opentaskpy/otflogging.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ def init_logging(
167167
# Set the log format
168168
formatter = logging.Formatter(OTF_LOG_FORMAT)
169169

170+
if os.environ.get("OTF_LOG_LEVEL") is not None:
171+
# Only use env var if level is still at default (logging.INFO)
172+
# This allows CLI args to override env vars
173+
if level == logging.INFO:
174+
level = os.environ["OTF_LOG_LEVEL"]
175+
170176
# If the task_id isn't set yet, then use the env var
171177
if not task_id:
172178
task_id = os.getenv("OTF_TASK_ID")
@@ -207,9 +213,6 @@ def init_logging(
207213

208214
# Set verbosity
209215
otf_logger.setLevel(logging.getLogger().getEffectiveLevel())
210-
# Ensure the logger is at least at INFO level
211-
if otf_logger.getEffectiveLevel() > logging.INFO:
212-
otf_logger.setLevel(logging.INFO)
213216

214217
# If the log level is set in the environment, then use that
215218
if os.environ.get("OTF_LOG_LEVEL") is not None:
@@ -232,7 +235,8 @@ def init_logging(
232235
otf_logger.addHandler(tfh)
233236
tfh.setFormatter(formatter)
234237

235-
otf_logger.debug("Logging initialised")
238+
if os.environ.get("OTF_LOG_INIT_EVENTS", None) == "1":
239+
otf_logger.debug("Logging initialised")
236240

237241
return otf_logger
238242

tests/test_logging.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,41 @@ def test_init_logging(env_vars, top_level_root_dir):
147147
assert len(logger.handlers) == 0
148148

149149

150+
def test_init_logging_logs_init_event_when_enabled(env_vars, tmpdir, caplog):
151+
os.environ["OTF_LOG_DIRECTORY"] = str(tmpdir)
152+
os.environ["OTF_LOG_LEVEL"] = "DEBUG"
153+
os.environ["OTF_LOG_INIT_EVENTS"] = "1"
154+
155+
with caplog.at_level(logging.DEBUG):
156+
logger = opentaskpy.otflogging.init_logging(
157+
"some.class.init_events", task_id="some_task_id", task_type="B"
158+
)
159+
160+
assert any(
161+
record.getMessage() == "Logging initialised" for record in caplog.records
162+
)
163+
164+
opentaskpy.otflogging.close_log_file(logger, True)
165+
166+
167+
def test_init_logging_env_log_level_sets_root_debug(env_vars):
168+
root_logger = logging.getLogger()
169+
original_level = root_logger.level
170+
original_handlers = list(root_logger.handlers)
171+
172+
os.environ["OTF_LOG_LEVEL"] = "DEBUG"
173+
os.environ["OTF_NO_LOG"] = "1"
174+
175+
opentaskpy.otflogging.init_logging(
176+
"some.class.root_logger", task_id="some_task_id", override_root_logger=True
177+
)
178+
179+
assert logging.getLogger().getEffectiveLevel() == logging.DEBUG
180+
181+
root_logger.setLevel(original_level)
182+
root_logger.handlers = original_handlers
183+
184+
150185
def test_get_latest_log_file(env_vars):
151186
# Setup some dummy log files
152187
log_path = "test/testLogs"

0 commit comments

Comments
 (0)