The log_print function prints an object to the currently opened log.

log_print(x, ..., console = TRUE, blank_after = NULL, msg = FALSE, hide_notes = FALSE)

put(x, ..., console = TRUE, blank_after = NULL, msg = FALSE, hide_notes = FALSE)

sep(x, console = TRUE)

log_hook(x)

Arguments

x

The object to print.

...

Any parameters to pass to the print function.

console

Whether or not to print to the console. Valid values are TRUE and FALSE. Default is TRUE.

blank_after

Whether or not to print a blank line following the printed object. The blank line helps readability of the log. Valid values are TRUE and FALSE. Default is TRUE.

msg

Whether to print the object to the msg log. This parameter is intended to be used internally. Value values are TRUE and FALSE. The default value is FALSE.

hide_notes

If notes are on, this parameter gives you the option of not printing notes for a particular log entry. Default is FALSE, meaning notes will be displayed. Used internally.

Value

The object, invisibly

Details

The log is initialized with log_open. Once the log is open, objects like variables and data frames can be printed to the log to monitor execution of your script. If working interactively, the function will print both to the log and to the console. The log_print function is useful when writing and debugging batch scripts, and in situations where some record of a scripts' execution is required.

If requested in the log_open function, log_print will print a note after each call. The note will contain a date-time stamp and elapsed time since the last call to log_print. When printing a data frame, the log_print function will also print the number and rows and column in the data frame. These counts may also be useful in debugging.

Notes may be turned off either by setting the show_notes parameter on log_open to FALSE, or by setting the global option "logr.notes" to FALSE.

The put function is a shorthand alias for log_print. You can use put anywhere you would use log_print. The functionality is identical.

The sep function is also a shorthand alias for log_print, except it will print a separator before and after the printed text. This function is intended for documentation purposes, and you can use it to help organize your log into sections.

The log_hook function is for other packages that wish to integrate with logr. The function prints to the log only if autolog is enabled. It will not print to the console.

See also

log_open to open the log, and log_close to close the log.

Examples

library(logr)

# Create temp file location
tmp <- file.path(tempdir(), "test.log")

# Open log
lf <- log_open(tmp)

# Send message to log
log_print("High Mileage Cars Subset")
#> High Mileage Cars Subset 

# Perform operations
hmc <- subset(mtcars, mtcars$mpg > 20)

# Print data to log
log_print(hmc)
#>                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#> Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#> Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#> Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

# Close log
log_close()

# View results
writeLines(readLines(lf))
#> ========================================================================= 
#> Log Path: /tmp/RtmpORwY5l/log/test.log 
#> Working Directory: /cluster-data/user-homes/elizabethb/projects/prism-pkgdocs-build/installed-pkgs/priority-3/logr_1.3.9/docs/reference 
#> User Name: elizabethb 
#> R Version: 4.4.1 (2024-06-14) 
#> Machine: uat-uat-a2ai-cloud x86_64 
#> Operating System: Linux 6.2.0-1018-aws #18~22.04.1-Ubuntu SMP Wed Jan 10 22:54:16 UTC 2024 
#> Base Packages: stats graphics grDevices utils datasets methods base 
#> Other Packages: logr_1.3.9 common_1.1.3 
#> Log Start Time: 2025-10-14 20:31:05.668143 
#> ========================================================================= 
#> 
#> High Mileage Cars Subset 
#> 
#> NOTE: Log Print Time:  2025-10-14 20:31:05.66994 
#> NOTE: Elapsed Time: 0.00136542320251465 secs 
#> 
#>                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#> Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#> Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#> Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
#> 
#> NOTE: Data frame has 14 rows and 11 columns. 
#> 
#> NOTE: Log Print Time:  2025-10-14 20:31:05.675725 
#> NOTE: Elapsed Time: 0.00578474998474121 secs 
#> 
#> ========================================================================= 
#> Log End Time: 2025-10-14 20:31:05.676843 
#> Log Elapsed Time: 0 00:00:00 
#> =========================================================================