Integrate the message printing function with the cli
package,
and the base::message function.
The message could be suppressed by base::suppressMessages.
Usage
log_message(
...,
verbose = TRUE,
message_type = c("info", "success", "warning", "error"),
cli_model = TRUE,
timestamp = TRUE,
timestamp_format = "%Y-%m-%d %H:%M:%S",
level = 1,
symbol = " ",
text_color = NULL,
back_color = NULL,
multiline_indent = TRUE,
.envir = parent.frame(),
.frame = .envir
)
Arguments
- ...
Text to print.
- verbose
Logical value, default is
TRUE
. Whether to print the message.- message_type
Type of message, default is
info
. Could be choose one ofinfo
,success
,warning
, anderror
.- cli_model
Logical value, default is
TRUE
. Whether to use thecli
package to print the message.- timestamp
Logical value, default is
TRUE
. Whether to show the current time in the message.- timestamp_format
Character value, default is
"%Y-%m-%d %H:%M:%S"
. Format string for timestamp display.- level
Integer value, default is
1
. The level of the message, which affects the indentation. Level 1 has no indentation, higher levels add more indentation.- symbol
Character value, default is
" "
(two spaces). The symbol used for indentation. When specified, it ignores the level parameter and uses the symbol directly.- text_color
Character value, default is
NULL
. Color for the message text. Available colors: "red", "green", "blue", "yellow", "magenta", "cyan", "white", "black".- back_color
Character value, default is
NULL
. Background color for the message text. Available colors: "red", "green", "blue", "yellow", "magenta", "cyan", "white", "black".- multiline_indent
Logical value, default is
TRUE
. Whether to apply consistent formatting (timestamp and indentation) to each line in multiline messages. When TRUE, each line gets the full formatting; when FALSE, only the first line gets the timestamp.- .envir
The environment to evaluate calls in. Default is
parent.frame()
.- .frame
The frame to use for error reporting. Default is
.envir
.
Examples
# basic usage
log_message("Hello, ", "world!")
#> ℹ [2025-07-06 07:15:17] Hello, world!
log_message("hello, world!")
#> ℹ [2025-07-06 07:15:17] Hello, world!
log_message("Hello, world!", timestamp = FALSE)
#> ℹ Hello, world!
log_message("Hello, ", "world!", message_type = "success")
#> ✔ [2025-07-06 07:15:17] Hello, world!
log_message("Hello, world!", message_type = "warning")
#> ! [2025-07-06 07:15:17] Hello, world!
log_message("Hello, ", "world!", cli_model = FALSE)
#> [2025-07-06 07:15:17] Hello, world!
# suppress messages
suppressMessages(log_message("Hello, world!"))
log_message("Hello, world!", verbose = FALSE)
# cli formatting
log_message("hello, {.arg world}!")
#> ℹ [2025-07-06 07:15:17] Hello, `world`!
message("hello, {.arg world}!")
#> hello, {.arg world}!
log_message("hello, {.code world}!")
#> ℹ [2025-07-06 07:15:17] Hello, `world`!
message("hello, {.code world}!")
#> hello, {.code world}!
log_message("{.emph R}")
#> ℹ [2025-07-06 07:15:17] R
log_message("Hello, {.file world}!")
#> ℹ [2025-07-06 07:15:17] Hello, world!
log_message("press {.kbd ENTER}")
#> ℹ [2025-07-06 07:15:17] Press [ENTER]
log_message("address: {.email example@example.com}")
#> ℹ [2025-07-06 07:15:17] Address: example@example.com
log_message("URL: {.url https://example.com}")
#> ℹ [2025-07-06 07:15:17] URL: <https://example.com>
log_message("Some {.field field}")
#> ℹ [2025-07-06 07:15:17] Some field
log_message("Hello, {.pkg world}!")
#> ℹ [2025-07-06 07:15:17] Hello, world!
log_message("Hello, {.val world}!")
#> ℹ [2025-07-06 07:15:17] Hello, "world"!
# set indentation
log_message("Hello, world!", level = 2)
#> ℹ [2025-07-06 07:15:17] Hello, world!
log_message("Hello, world!", symbol = "->")
#> ℹ [2025-07-06 07:15:17] -> Hello, world!
# multiline message
log_message("Line 1\nLine 2\nLine 3", multiline_indent = TRUE)
#> ℹ [2025-07-06 07:15:17] Line 1
#> ℹ [2025-07-06 07:15:17] Line 2
#> ℹ [2025-07-06 07:15:17] Line 3
log_message(
"Multi-line\ncolored\nmessage",
text_color = "blue",
multiline_indent = FALSE
)
#> ℹ [2025-07-06 07:15:17] Multi-line
#> colored
#> message
log_message(
"Multi-line\ncolored\nmessage",
text_color = "blue",
multiline_indent = FALSE,
timestamp = FALSE
)
#> ℹ Multi-line
#> colored
#> message
# color formatting
log_message(
"This is a red message",
text_color = "red"
)
#> ℹ [2025-07-06 07:15:17] This is a red message
log_message(
"This is a message with background",
back_color = "cyan"
)
#> ℹ [2025-07-06 07:15:17] This is a message with background
log_message(
"This is a message with both text and background",
text_color = "red",
back_color = "cyan"
)
#> ℹ [2025-07-06 07:15:17] This is a message with both text and background
# color formatting also works with `cli_model = FALSE`
log_message(
"This is a red message",
text_color = "red",
cli_model = FALSE
)
#> [2025-07-06 07:15:17] This is a red message
log_message(
"This is a message with background",
back_color = "cyan",
cli_model = FALSE
)
#> [2025-07-06 07:15:17] This is a message with background
log_message(
"This is a message with both text and background",
text_color = "red",
back_color = "cyan",
cli_model = FALSE
)
#> [2025-07-06 07:15:17] This is a message with both text and background
# cli variables
fun <- function() {
a <- 1
log_message("{.val a}")
log_message("{.val {a}}")
log_message("{.val {a + 1}}")
}
fun()
#> ℹ [2025-07-06 07:15:17] "a"
#> ℹ [2025-07-06 07:15:17] "1"
#> ℹ [2025-07-06 07:15:17] "2"