Skip to contents

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 of info, success, warning, and error.

cli_model

Logical value, default is TRUE. Whether to use the cli 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.

Value

Formated message printed to the console.

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"