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

DT-20: active get log level #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion .github/workflows/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: pkgdown
on: push
on:
push:
branches:
- master
permissions:
contents: write
jobs:
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: stenographer
Type: Package
Title: Flexible and Customisable Logging System
Version: 1.0.0
Version: 1.0.1
URL: https://github.com/dereckmezquita/stenographer
BugReports: https://github.com/dereckmezquita/stenographer/issues
Authors@R:
Expand Down
20 changes: 20 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# stenographer [v1.1.0](https://github.com/dereckmezquita/stenographer/milestone/2)

## BREAKING CHANGES

- **N/A**

## NEW FEATURES

1. **Access set LogLevel:** Use `Stenographer$get_level` to read the current logging level; active field.

## IMPROVEMENTS

1. **Stenographer$set_level method checks value:** The `set_level` method now validates the input value to ensure it is a valid logging level.

## DOCUMENTATION

## DEVELOPMENT

## NOTES

# stenographer [v1.0.0](https://github.com/dereckmezquita/stenographer/milestone/1) (12 January 2025)

## BREAKING CHANGES
Expand Down
16 changes: 16 additions & 0 deletions R/Stenographer.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ LogLevel <- list(
INFO = 2L
)

#' @title Check if an object is a valid log level
#' @param x Object to check
#' @return Logical
valid_log_level <- function(x) {
return(is.integer(x) && (x %in% c(-1L, 0L, 1L, 2L)))
}

#' @title R6 Class for Advanced Logging Functionality
#'
#' @description
Expand All @@ -41,6 +48,7 @@ LogLevel <- list(
#' * Contextual data attachment
#' * Coloured console output
#'
#' @importFrom rlang abort
#' @importFrom R6 R6Class
#' @importFrom fs path_dir dir_exists dir_create file_exists file_create
#' @importFrom DBI dbExistsTable dbExecute dbWriteTable
Expand Down Expand Up @@ -79,6 +87,11 @@ LogLevel <- list(
#' @export
Stenographer <- R6Class(
"Stenographer",
active = list(
#' @field Get log level (read-only)
get_level = function() return(private$level)
),

public = list(
#' @description
#' Create a new Stenographer instance
Expand Down Expand Up @@ -121,6 +134,9 @@ Stenographer <- R6Class(
#' Update the minimum logging level
#' @param level New log level (see `LogLevel`)
set_level = function(level) {
if (!valid_log_level(level)) {
abort("Invalid log level")
}
private$level <- level
},

Expand Down
14 changes: 14 additions & 0 deletions man/Stenographer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/valid_log_level.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 14 additions & 16 deletions tests/testthat/test-Stenographer.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,26 @@ test_that("Stenographer respects LogLevel$INFO", {
expect_true(any(grepl("test error", output)))
})

test_that("set_level changes logging behavior", {
output <- character(0)
steno <- Stenographer$new(
level = LogLevel$INFO,
print_fn = function(x) output <<- c(output, x)
)

# Start with INFO level
steno$info("test1")
expect_length(output, 1)
test_that("Stenographer active field get_level returns correct level and set_level works", {
# Start with INFO
steno <- Stenographer$new(level = LogLevel$INFO)
expect_equal(steno$get_level, LogLevel$INFO)

# Change to OFF
steno$set_level(LogLevel$OFF)
steno$info("test2")
steno$error("test3")
expect_length(output, 1) # No new messages
expect_equal(steno$get_level, LogLevel$OFF)

# Change to ERROR
steno$set_level(LogLevel$ERROR)
steno$info("test4")
steno$error("test5")
expect_length(output, 2) # Only error added
expect_equal(steno$get_level, LogLevel$ERROR)

# Change to WARNING
steno$set_level(LogLevel$WARNING)
expect_equal(steno$get_level, LogLevel$WARNING)

# Change back to INFO
steno$set_level(LogLevel$INFO)
expect_equal(steno$get_level, LogLevel$INFO)
})

test_that("context management works correctly", {
Expand Down
Loading