---
title: "Logging and errors"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Logging and errors}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(eider)
```

## Logging in `eider`

`eider` uses [the `logger` package](https://daroczig.github.io/logger/index.html) to log messages.
Most messages in `eider` are logged at either the `DEBUG` level (when `eider` is e.g. parsing information from JSON), or at the `TRACE` level (most functions in `eider` log a `TRACE` message containing the execution context).

When running a pipeline, you can set the logging level with either:

```{r eval=FALSE}
logger::log_threshold(logger::DEBUG)
```

which causes the `DEBUG` messages to be displayed, or:

```{r eval=FALSE}
logger::log_threshold(logger::TRACE)
```

which causes both the `TRACE` and `DEBUG` messages to be displayed.

## Errors with context

Additionally, the execution context (which is usually restricted to `TRACE` messages) is also displayed when `eider` runs into an error.
If you run into an error that does not provide enough information, please consider [submitting an issue](https://github.com/alan-turing-institute/eider/issues).

Here are a few examples:

### Wrong transformation type

```{r comment='', echo=FALSE}
writeLines(readLines("json_examples/logging1.json"))
```

In the JSON above ([`json_examples/logging1.json`](https://github.com/alan-turing-institute/eider/blob/main/vignettes/json_examples/logging1.json)), an invalid `transformation_type` is specified.
Notice how the resulting error tells you which JSON file the error occurs in.

```{r error=TRUE}
run_pipeline(
  data_sources = list(ae2 = eider_example("random_ae_data.csv")),
  feature_filenames = "json_examples/logging1.json"
)
```

### Wrong column name

```{r comment='', echo=FALSE}
writeLines(readLines("json_examples/logging2.json"))
```

Here ([`json_examples/logging2.json`](https://github.com/alan-turing-institute/eider/blob/main/vignettes/json_examples/logging2.json)), a `grouping_column` is specified, but such a column does not exist in the input table.

```{r error=TRUE}
run_pipeline(
  data_sources = list(ae2 = eider_example("random_ae_data.csv")),
  feature_filenames = "json_examples/logging2.json"
)
```

### Data type mismatch

```{r comment='', echo=FALSE}
writeLines(readLines("json_examples/logging3.json"))
```

This example ([`json_examples/logging3.json`](https://github.com/alan-turing-institute/eider/blob/main/vignettes/json_examples/logging3.json)) specifies that the table should be filtered to only retain rows where `diagnosis_1` is equal to `"a string"`, but in the actual table, `diagnosis_1` is an integer.

```{r error=TRUE}
run_pipeline(
  data_sources = list(ae2 = eider_example("random_ae_data.csv")),
  feature_filenames = "json_examples/logging3.json"
)
```
