---
title: "Logging"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{logging}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
knitr::opts_knit$set(root.dir = tempdir())
```
```{r setup}
library(ptspotter)
```
## Logging with 'log4r'
Logging is an excellent way to monitor the status of your data pipelines. Please consult the ['log4r' documentation ](https://CRAN.R-project.org/package=log4r) for a comprehensive
guide to its features.
## Logging File Operations
In order to quickly get up and running with 'log4r', there are a few steps to
take first. A directory and a logfile are required.
```{r}
log_file_ops(dir_path = "logs", logfile_nm = "logfile")
list.files("logs")
```
This will only need to be executed once. Running this line again will throw
an error.
```{r, error=TRUE}
log_file_ops(dir_path = "logs", logfile_nm = "logfile")
```
## Logger Objects
Next we need to create the required objects within R to be able to write to the
logfile we created. Namely, a **file appender** and **logger** objects are
needed. This needs to be executed every time you run your programme, so include
it within your script.
```{r}
log_enable(logfile_loc = "logs/logfile.txt")
```
## Start Logging
We can now use the logging functions in 'log4r' to begin writing messages to the
logfile.
```{r}
log4r::info(logger = my_logger, message = "Some info")
log4r::warn(logger = my_logger, message = "Some warning")
log4r::error(logger = my_logger, message = "Some error")
# Check the messages are being logged
readLines("logs/logfile.txt")
```
```{r, include=FALSE}
unlink("logs", recursive = TRUE)
```