Skip to content

Commit

Permalink
Funksjon for å aktivere logging i json-format
Browse files Browse the repository at this point in the history
La det inn i fila appLog.R. Det som lå der var en definisjon noe data i pakken. Denne definisjonen flyttet jeg til data.R
  • Loading branch information
arnfinn committed Nov 22, 2024
1 parent 5bef250 commit e046b42
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 38 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Imports:
jsonlite,
kableExtra,
knitr,
logger,
magrittr,
readr,
rlang,
Expand All @@ -50,7 +51,7 @@ Imports:
sship (>= 0.9.0),
utils,
yaml
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
URL: https://github.com/Rapporteket/rapbase
BugReports: https://github.com/Rapporteket/rapbase/issues
Suggests:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export(loadRegData)
export(loadStagingData)
export(logFormat)
export(logTimeFrame)
export(loggerSetup)
export(makeAutoReportTab)
export(makeRunDayOfYearSequence)
export(mst)
Expand Down
44 changes: 31 additions & 13 deletions R/appLog.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
#' App log test dataset.
#' Settings for logging as json
#'
#' A dataset containing test entries for the application log.
#' Every info, warning and error will be logged in json format.
#'
#' @format A data frame with 20 rows and 7 variables:
#' \describe{
#' \item{time}{character timestamp}
#' \item{user}{user name}
#' \item{name}{user full name}
#' \item{group}{users group/registry}
#' \item{role}{users role}
#' \item{resh_id}{users organization}
#' \item{message}{log message}
#' }
"appLog"
#' @export
#'
loggerSetup <- function(
usernameEnv = "SHINYPROXY_USERNAME",

Check warning on line 8 in R/appLog.R

View workflow job for this annotation

GitHub Actions / lint

file=R/appLog.R,line=8,col=4,[indentation_linter] Indentation should be 2 spaces but is 4 spaces.
appidEnv = "SHINYPROXY_APPID",
testing = FALSE
) {
logger::log_threshold(logger::INFO)
formatterJson <- function(level, message, ...) {
username <- Sys.getenv(usernameEnv, unset = "unknown")
appid <- Sys.getenv(appidEnv, unset = "unknown")
return(jsonlite::toJSON(
list(
time = format(Sys.time(), "%Y-%m-%d %H:%M:%OS3"),

Check warning on line 18 in R/appLog.R

View workflow job for this annotation

GitHub Actions / lint

file=R/appLog.R,line=18,col=8,[indentation_linter] Hanging indent should be 11 spaces but is 8 spaces.
level = attr(level, "level"),
app = appid,
user = username,
message = message),
auto_unbox = TRUE
)
)
}
logger::log_layout(formatterJson)
if (!testing) {
logger::log_messages()
logger::log_warnings()
logger::log_errors()

Check warning on line 31 in R/appLog.R

View check run for this annotation

Codecov / codecov/patch

R/appLog.R#L29-L31

Added lines #L29 - L31 were not covered by tests
}
}
15 changes: 15 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#' App log test dataset.
#'
#' A dataset containing test entries for the application log.
#'
#' @format A data frame with 20 rows and 7 variables:
#' \describe{
#' \item{time}{character timestamp}
#' \item{user}{user name}
#' \item{name}{user full name}
#' \item{group}{users group/registry}
#' \item{role}{users role}
#' \item{resh_id}{users organization}
#' \item{message}{log message}
#' }
"appLog"
2 changes: 1 addition & 1 deletion man/appLog.Rd

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

15 changes: 15 additions & 0 deletions man/loggerSetup.Rd

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

19 changes: 0 additions & 19 deletions man/rapbase.Rd

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

43 changes: 39 additions & 4 deletions tests/testthat/test-log.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ test_that("append and read errors when target is not known", {
# remove test db
if (is.null(check_db(is_test_that = FALSE))) {
con <- RMariaDB::dbConnect(RMariaDB::MariaDB(),
host = Sys.getenv("DB_HOST"),
user = Sys.getenv("DB_USER"),
password = Sys.getenv("DB_PASS"),
bigint = "integer"
host = Sys.getenv("DB_HOST"),
user = Sys.getenv("DB_USER"),
password = Sys.getenv("DB_PASS"),
bigint = "integer"
)
RMariaDB::dbExecute(con, paste("DROP DATABASE", nameLogDb))
rapbase::rapCloseDbConnection(con)
Expand All @@ -225,3 +225,38 @@ if (is.null(check_db(is_test_that = FALSE))) {
# Restore instance
Sys.setenv(R_RAP_CONFIG_PATH = currentConfig)
Sys.setenv(R_RAP_INSTANCE = currentInstance)

test_that("loggerSetup is working", {
# env-stuff
currentUser <- Sys.getenv("SHINYPROXY_USERNAME")
currentApp <- Sys.getenv("SHINYPROXY_APPID")
Sys.setenv(SHINYPROXY_USERNAME = "jesus@sky.com")
Sys.setenv(SHINYPROXY_APPID = "rapbasis")

# run the function we want to test
loggerSetup(testing = TRUE)

# log something
infoLogjson <- logger::log_info(
"Test log setup",
namespace = "tmp"
)$default$record

# Test what has been logged
testthat::expect_true(jsonlite::validate(infoLogjson))
infoLog <- jsonlite::fromJSON(infoLogjson)
testthat::expect_equal(nchar(infoLog$time), 23)
testthat::expect_equal(infoLog$level, "INFO")
testthat::expect_equal(infoLog$message, "Test log setup")
testthat::expect_equal(infoLog$app, "rapbasis")
testthat::expect_equal(infoLog$user, "jesus@sky.com")

# env-stuff
if (currentUser == "" && currentApp == "") {
Sys.unsetenv("SHINYPROXY_USERNAME")
Sys.unsetenv("SHINYPROXY_APPID")
} else {
Sys.setenv(SHINYPROXY_USERNAME = currentUser)
Sys.setenv(SHINYPROXY_APPID = currentApp)
}
})
Binary file added tests/testthat/tmp.rds
Binary file not shown.

0 comments on commit e046b42

Please sign in to comment.