Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update logging to always output to STDOUT/STDERR by default #179

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ecc5285
update CLI logger option to log to the STDOUT by default.
hollandjg Oct 28, 2024
a0fa093
update variable name for log paths
hollandjg Oct 28, 2024
9014361
fix missing symbol
hollandjg Oct 28, 2024
46c2089
add example shell script to run the pipeline functions from the comma…
hollandjg Oct 28, 2024
c91be90
remove teelogger when we're just using the global logger
hollandjg Oct 28, 2024
9576e81
don't exclude error level logs
hollandjg Oct 28, 2024
a13a73d
example of using teeing rather than the --log argument
hollandjg Oct 28, 2024
c34a201
Revert "example of using teeing rather than the --log argument"
hollandjg Oct 28, 2024
9205b84
add working debug logger
hollandjg Oct 28, 2024
445c2bc
switch to using just --debug for logging
hollandjg Oct 28, 2024
38762e8
import logging
hollandjg Oct 29, 2024
326b70d
demo piping all the stdout and stderr to file
hollandjg Oct 29, 2024
7709fcd
example running debugging mode
hollandjg Oct 29, 2024
10bcc9a
remove unnecessary logging machinery from CLI
hollandjg Oct 29, 2024
3f27ea6
remove extra JULIA_DEBUG from test script
hollandjg Oct 29, 2024
89a1987
remove logging setup
hollandjg Oct 29, 2024
0e51d70
add readme section on logging.
hollandjg Oct 29, 2024
a4ce896
update test script
hollandjg Oct 29, 2024
f72572c
remove logging dependencies
hollandjg Oct 29, 2024
88f89ae
Merge branch 'jghrefactor/A1-update-dockerfile' into jghrefactor/A2-u…
hollandjg Oct 29, 2024
902911e
Merge branch 'jghrefactor/A1-update-dockerfile' into jghrefactor/A2-u…
hollandjg Oct 29, 2024
f6ec233
Merge branch 'jghrefactor/A1-update-dockerfile' into jghrefactor/A2-u…
hollandjg Oct 30, 2024
b21584d
Merge branch 'jghrefactor/A2-update-logging' of https://github.com/Wi…
hollandjg Nov 4, 2024
d30048c
Merge branch 'jghrefactor/A1-update-dockerfile' into jghrefactor/A2-u…
hollandjg Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 7 additions & 44 deletions IFTPipeline.jl/src/cli.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env julia

using ArgParse
using LoggingExtras
using IceFloeTracker
using IFTPipeline
using Serialization
Expand Down Expand Up @@ -228,34 +227,6 @@ function mkcli!(settings, common_args)
end


"""
setuplogger(option::Int64, path::String)

Setup logger for the ice floe tracker. If `option` is 0, log to file only. If `option` is 1, log to file and terminal.
"""
function setuplogger(option::Int64, command::Symbol)
output = joinpath(@__DIR__, "..", "report")
cmd = string(command)

filelogger = FileLogger(joinpath(output, "$cmd-logfile.log")) # add command prefix to logfile name

# filter out debug messages
filtlogger = EarlyFilteredLogger(filelogger) do args
r = Logging.Info <= args.level <= Logging.Warn && args._module === IFTPipeline
return r
end

if option == 0
return TeeLogger(filtlogger,
)
elseif option == 1
return TeeLogger(
global_logger(),
filtlogger
)
end
end

function main()

settings = ArgParseSettings(; autofix_names=true)
Expand All @@ -281,12 +252,9 @@ function main()
help = "Pair ice floes in day k with ice floes in day k+1"
action = :command
end

command_common_args = [
"--log",
Dict(:help => "Show log on terminal; either 1 or 0", :required => false, :arg_type => Int,
:default => 0, :range_tester => (x -> x == 0 || x == 1)
)]

command_common_args = []


mkcli!(settings, command_common_args)

Expand All @@ -295,16 +263,11 @@ function main()
command = parsed_args[:_COMMAND_]
command_args = parsed_args[command]
command_func = getfield(IFTPipeline, Symbol(command))
logoption = command_args[:log]

# delete log option from command_args so it doesn't get passed to command_func
delete!(command_args, :log)

logger = setuplogger(logoption, command)

with_logger(logger) do
@time command_func(; command_args...)
end
@debug "debug message"
@info "info message"
@time command_func(; command_args...)

return nothing
end

Expand Down
25 changes: 25 additions & 0 deletions IFTPipeline.jl/test/_test-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# Scripts
IFT="julia --project=../ ../src/cli.jl"

# Data source and target
DATA_SOURCE=test_inputs/input_pipeline
TEMPDIR=$(mktemp -d -p __temp__)
echo "TEMPDIR=${TEMPDIR}"

# Initialize data directory
cp ${DATA_SOURCE}/20220914.{terra,aqua}.{true,false}color.250m.tiff ${TEMPDIR}
cp ${DATA_SOURCE}/landmask.tiff ${TEMPDIR}
cp ${DATA_SOURCE}/passtimes_lat.csv ${TEMPDIR}
SAMPLEIMG=${TEMPDIR}/20220914.terra.truecolor.250m.tiff

# Set up debug messages
export JULIA_DEBUG="Main,IFTPipeline,IceFloeTracker"

# Run the processing
${IFT} landmask ${TEMPDIR} ${TEMPDIR}
${IFT} preprocess -t ${TEMPDIR} -r ${TEMPDIR} -l ${TEMPDIR} -p ${TEMPDIR} -o ${TEMPDIR}
${IFT} extractfeatures -i ${TEMPDIR} -o ${TEMPDIR}
${IFT} track --imgs ${TEMPDIR} --props ${TEMPDIR} --passtimes ${TEMPDIR} --latlon ${SAMPLEIMG} -o ${TEMPDIR}
${IFT} makeh5files --pathtosampleimg ${SAMPLEIMG} --resdir ${TEMPDIR}
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,38 @@ ift_segmented = np.ma.masked_array(ift_segmented, mask=ift_segmented==0)
fig, ax = plt.subplots()
ax.imshow(ift_segmented, cmap='prism')
```

## Logging

The command line tools all implement logging to `Stderr`.

To redirect Stdout and Stderr to a file, append `&> outfile.log` to the command, e.g.:
```bash
julia --project=IFTPipeline.jl IFTPipeline.jl/src/cli.jl landmask ./input_directory/ ./output_directory/ &> outfile.log
```

To print the output to the console in addition to writing it to a file, append `|& tee outfile.log` to the command, e.g.:
```bash
julia --project=IFTPipeline.jl IFTPipeline.jl/src/cli.jl landmask ./input_directory/ ./output_directory/ |& tee outfile.log
```

This works the same way for all the command line tools, including those in Julia, Python, and Bash.

When used within the cylc workflow, the logs are automatically saved to the `cylc-run` directory
and can be viewed using the `cylc tui` or `cylc gui` or viewed directly from the terminal, e.g.:
```bash
cat ./cylc-run/<workflow-name>/<run#>/log/job/1/<task-name>/01/job.err
```

Debug messages (from `@debug` macros in the code) can be activated for
each command line task by calling Julia with the `JULIA_DEBUG` environment variable set.

To activate debug logging for the Ice Floe Tracker, call:
```bash
JULIA_DEBUG="Main,IFTPipeline,IceFloeTracker" julia --project=IFTPipeline.jl IFTPipeline.jl/src/cli.jl landmask
```

To activate debug logging for all the packages used (many more messages), call:
```bash
JULIA_DEBUG="all" julia --project=IFTPipeline.jl IFTPipeline.jl/src/cli.jl landmask ...
```