From 4cea7d63f1b0c717c27b54c7f51e26cce365fca2 Mon Sep 17 00:00:00 2001 From: Dereck Mezquita <44912288+dereckmezquita@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:38:05 -0500 Subject: [PATCH] DT-20: active get log level (#5) * DT-20: only rebuild docs on merge to master. * DT-20: active field for getting log level. --- .github/workflows/pkgdown.yml | 5 ++++- DESCRIPTION | 2 +- NEWS.md | 20 ++++++++++++++++++++ R/Stenographer.R | 16 ++++++++++++++++ man/Stenographer.Rd | 14 ++++++++++++++ man/valid_log_level.Rd | 17 +++++++++++++++++ tests/testthat/test-Stenographer.R | 30 ++++++++++++++---------------- 7 files changed, 86 insertions(+), 18 deletions(-) create mode 100644 man/valid_log_level.Rd diff --git a/.github/workflows/pkgdown.yml b/.github/workflows/pkgdown.yml index 5416ba2..8437afe 100644 --- a/.github/workflows/pkgdown.yml +++ b/.github/workflows/pkgdown.yml @@ -1,5 +1,8 @@ name: pkgdown -on: push +on: + push: + branches: + - master permissions: contents: write jobs: diff --git a/DESCRIPTION b/DESCRIPTION index 75a744c..0651489 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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: diff --git a/NEWS.md b/NEWS.md index 19d16c5..6b21df1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/Stenographer.R b/R/Stenographer.R index 53cdaf9..705859c 100644 --- a/R/Stenographer.R +++ b/R/Stenographer.R @@ -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 @@ -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 @@ -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 @@ -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 }, diff --git a/man/Stenographer.Rd b/man/Stenographer.Rd index daea05c..62f2210 100644 --- a/man/Stenographer.Rd +++ b/man/Stenographer.Rd @@ -43,6 +43,20 @@ custom_steno$set_level(LogLevel$INFO) custom_steno$update_context(list(user = "John")) custom_steno$info("Now this will be logged with a custom prefix and context") +} +\section{Public fields}{ +\if{html}{\out{
}} +\describe{ +\item{\code{Get}}{log level (read-only)} +} +\if{html}{\out{
}} +} +\section{Active bindings}{ +\if{html}{\out{
}} +\describe{ +\item{\code{Get}}{log level (read-only)} +} +\if{html}{\out{
}} } \section{Methods}{ \subsection{Public methods}{ diff --git a/man/valid_log_level.Rd b/man/valid_log_level.Rd new file mode 100644 index 0000000..352bd9d --- /dev/null +++ b/man/valid_log_level.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Stenographer.R +\name{valid_log_level} +\alias{valid_log_level} +\title{Check if an object is a valid log level} +\usage{ +valid_log_level(x) +} +\arguments{ +\item{x}{Object to check} +} +\value{ +Logical +} +\description{ +Check if an object is a valid log level +} diff --git a/tests/testthat/test-Stenographer.R b/tests/testthat/test-Stenographer.R index 0a4e21c..5816812 100644 --- a/tests/testthat/test-Stenographer.R +++ b/tests/testthat/test-Stenographer.R @@ -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", {