Package 'pocketlogR'

Title: Log Application Events to PocketBase from R
Description: A unified operational monitoring system that logs application events (data jobs, website monitoring, email checks, database checks, etc.) to a PocketBase instance. Flows carry owner, description, schedule, and arbitrary JSON metadata. Supports DAG-based flow dependencies with cascade health checks and two authentication modes (regular user and superuser).
Authors: Quinten Goens [aut, cre]
Maintainer: Quinten Goens <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2026-12-05 20:32:04 UTC
Source: https://github.com/euctrl-pru/pocketlogR

Help Index


Add upstream dependencies to a flow

Description

Adds one or more upstream flows as dependencies of the target flow. Validates that all named flows exist and that no cycle would be created.

Usage

pl_add_dependency(conn, flow, depends_on)

Arguments

conn

A connection object from pl_connect().

flow

Name of the flow to update.

depends_on

Character vector of upstream flow names to add.

Value

Invisibly returns the updated flow record.

Examples

## Not run: 
conn <- pl_connect()
pl_add_dependency(conn, "ans_data_freshness", "another_upstream_flow")

## End(Not run)

Connect to PocketBase as a regular user

Description

Authenticates against a PocketBase users auth collection and returns a connection object for use with all daily pocketlogR functions.

Usage

pl_connect(url = NULL, email = NULL, password = NULL)

Arguments

url

PocketBase instance URL. Defaults to POCKETLOG_URL env var.

email

Service account email. Defaults to POCKETLOG_EMAIL env var.

password

Service account password. Defaults to POCKETLOG_PASSWORD env var.

Value

A named list with elements url (character) and token (character JWT).

Examples

## Not run: 
conn <- pl_connect()
conn <- pl_connect(
  url      = "https://myapp.pockethost.io",
  email    = "[email protected]",
  password = "secret"
)

## End(Not run)

Connect to PocketBase as a superuser (admin)

Description

Authenticates against the ⁠_superusers⁠ collection. Use this only for pl_setup(). Regular operations should use pl_connect().

Usage

pl_connect_admin(url = NULL, email = NULL, password = NULL)

Arguments

url

PocketBase instance URL. Defaults to POCKETLOG_URL env var.

email

Superuser email. Defaults to POCKETLOG_ADMIN_EMAIL env var.

password

Superuser password. Defaults to POCKETLOG_ADMIN_PASSWORD env var.

Value

A named list with elements url (character) and token (character JWT).

Examples

## Not run: 
conn_admin <- pl_connect_admin()

## End(Not run)

Create a new flow

Description

Registers a new flow in PocketBase. Errors if a flow with the same name already exists. Validates that depends_on flows exist and that adding them would not create a cycle in the DAG.

Usage

pl_create_flow(
  conn,
  name,
  type,
  owner,
  description = NULL,
  schedule = NULL,
  metadata = NULL,
  depends_on = NULL
)

Arguments

conn

A connection object from pl_connect().

name

Unique flow identifier (e.g. "ectrl_data_load").

type

Flow type string. See pl_flow_types for defaults, but any string is accepted.

owner

Free-text owner or responsible party (e.g. "quinten", "team-data").

description

Optional human-readable description.

schedule

Optional cron expression or human-readable schedule string.

metadata

Optional named list of arbitrary data, serialised to JSON (e.g. list(url = "https://...", region = "EU")).

depends_on

Optional character vector of upstream flow names.

Value

Invisibly returns the created flow record as a list.

Examples

## Not run: 
conn <- pl_connect()
pl_create_flow(conn, "ectrl_data_load", type = "data_job", owner = "quinten",
               description = "Daily EUROCONTROL data import",
               schedule = "0 6 * * *")

## End(Not run)

Delete a flow (admin only)

Description

Deletes a flow by name. Requires a superuser connection. Because PocketBase enforces referential integrity, any log entries referencing the flow must be deleted first. Use force = TRUE to do this automatically.

Usage

pl_delete_flow(conn, flow, force = FALSE)

Arguments

conn

A superuser connection object from pl_connect_admin().

flow

Flow name (character).

force

If TRUE, deletes all log entries for this flow before deleting the flow itself. If FALSE (default), errors if log entries exist.

Value

Invisibly returns NULL.

Examples

## Not run: 
conn_admin <- pl_connect_admin()
pl_delete_flow(conn_admin, "old_flow", force = TRUE)

## End(Not run)

Delete log entries (admin only)

Description

Deletes log entries. Requires a superuser connection. All filter arguments are optional and combined with AND logic. Called with no filters, deletes all log entries.

Usage

pl_delete_logs(conn, flow = NULL, before = NULL, status = NULL)

Arguments

conn

A superuser connection object from pl_connect_admin().

flow

Optional flow name. If provided, only logs for that flow are deleted.

before

Optional POSIXct or ISO 8601 string. If provided, only logs created before this timestamp are deleted.

status

Optional status string ("SUCCESS", "ERROR", or "FATAL"). If provided, only logs with that status are deleted.

Value

Invisibly returns the number of deleted records.

Examples

## Not run: 
conn_admin <- pl_connect_admin()

# Delete all logs older than 30 days
pl_delete_logs(conn_admin, before = Sys.time() - 30 * 86400)

# Delete all error logs for a specific flow
pl_delete_logs(conn_admin, flow = "ectrl_data_load", status = "ERROR")

# Delete all logs for a flow (used internally by pl_delete_flow(force=TRUE))
pl_delete_logs(conn_admin, flow = "old_flow")

## End(Not run)

Log an ERROR event

Description

Convenience wrapper around pl_log() for recoverable errors.

Usage

pl_error(
  conn,
  flow,
  log_type,
  message = NULL,
  metadata = NULL,
  logged_by = NULL,
  logged_from = NULL,
  source_file = NULL,
  source_repo = NULL
)

Arguments

conn

A connection object from pl_connect().

flow

Flow name (character).

log_type

Free-text log type (e.g. "data_job", "website_online"). See pl_log_types for standard values, but any string is accepted.

message

Optional human-readable log message.

metadata

Optional named list, serialized to JSON.

logged_by

Optional username of who created this log. If NULL (default), auto-detected from the OS (works on Windows, macOS, and Linux).

logged_from

Optional named list with machine context (e.g. list(machine = "...", os = "...", user = "...")). If NULL (default), auto-detected via Sys.info(). Stored as JSON.

source_file

Optional filename of the script creating this log. If NULL (default), auto-detected from the R call stack or Rscript --file argument. Returns NA in interactive sessions with no source()d file.

source_repo

Optional git repository name where the calling script lives. If NULL (default), auto-detected from the origin remote of the current working directory's git repo.

Value

Invisibly returns the created log record, or NULL on failure.

Examples

## Not run: 
pl_error(conn, "ans_website_online", log_type = "website_online",
         message = "HTTP 503 returned",
         metadata = list(http_status = 503))

## End(Not run)

Log a FATAL event

Description

Convenience wrapper around pl_log() for unrecoverable failures.

Usage

pl_fatal(
  conn,
  flow,
  log_type,
  message = NULL,
  metadata = NULL,
  logged_by = NULL,
  logged_from = NULL,
  source_file = NULL,
  source_repo = NULL
)

Arguments

conn

A connection object from pl_connect().

flow

Flow name (character).

log_type

Free-text log type (e.g. "data_job", "website_online"). See pl_log_types for standard values, but any string is accepted.

message

Optional human-readable log message.

metadata

Optional named list, serialized to JSON.

logged_by

Optional username of who created this log. If NULL (default), auto-detected from the OS (works on Windows, macOS, and Linux).

logged_from

Optional named list with machine context (e.g. list(machine = "...", os = "...", user = "...")). If NULL (default), auto-detected via Sys.info(). Stored as JSON.

source_file

Optional filename of the script creating this log. If NULL (default), auto-detected from the R call stack or Rscript --file argument. Returns NA in interactive sessions with no source()d file.

source_repo

Optional git repository name where the calling script lives. If NULL (default), auto-detected from the origin remote of the current working directory's git repo.

Value

Invisibly returns the created log record, or NULL on failure.

Examples

## Not run: 
pl_fatal(conn, "ectrl_data_load", log_type = "data_job",
         message = "Unrecoverable database error")

## End(Not run)

Default flow types

Description

A character vector of the default flow type identifiers used by pocketlogR. These are not enforced — any string is accepted as a type. They exist for documentation, consistency, and convenience.

Usage

pl_flow_types

Get a full DAG overview of all flows and their health

Description

Returns a tibble (data.frame) of ALL flows with both their raw (most recent log) status and effective (cascade-aware) status. A flow is considered "poisoned" if any upstream dependency has a more recent ERROR/FATAL log than the flow's own last SUCCESS log — meaning the flow ran successfully before the upstream broke.

Usage

pl_get_dag(conn, since = NULL)

Arguments

conn

A connection object from pl_connect().

since

Optional POSIXct or ISO 8601 string. If provided, only considers log entries created after this timestamp.

Details

Poisoning rule: A downstream flow is poisoned if:

  1. It has at least one upstream flow with an ERROR or FATAL log.

  2. The downstream's last SUCCESS log is MORE RECENT than that upstream failure. (i.e., the downstream ran successfully after the upstream had already broken.)

  3. The downstream itself is not already ERROR or FATAL.

If the downstream has never logged, effective_status is NA (unknown), regardless of upstream status.

Value

A data.frame with columns:

  • flow (character): flow name

  • type (character): flow type

  • schedule (character): schedule string

  • raw_status (character): most recent log status (NA if never logged)

  • raw_status_time (character): timestamp of most recent log

  • effective_status (character): "SUCCESS", "ERROR", "FATAL", "POISONED", or NA if never logged

  • poisoned_by (list-column): character vector of upstream flow names that caused poisoning

  • depends_on (list-column): character vector of direct upstream flow names

  • is_root (logical): TRUE if the flow has no upstream dependencies

Examples

## Not run: 
conn <- pl_connect()
dag <- pl_get_dag(conn)
dag

## End(Not run)

Get upstream dependencies of a flow

Description

Returns a data.frame of upstream flows for the named flow.

Usage

pl_get_dependencies(conn, flow, recursive = FALSE)

Arguments

conn

A connection object from pl_connect().

flow

Flow name.

recursive

If TRUE, walks the full DAG upward and returns all transitive upstream dependencies. If FALSE (default), returns only direct (immediate) upstream dependencies.

Value

A data.frame with columns: name, type, description, schedule, depth.

Examples

## Not run: 
conn <- pl_connect()
pl_get_dependencies(conn, "ans_monthly_update")
pl_get_dependencies(conn, "ans_monthly_update", recursive = TRUE)

## End(Not run)

List flows

Description

Returns a data.frame of flows, optionally filtered by type or name.

Usage

pl_get_flows(conn, type = NULL, name = NULL)

Arguments

conn

A connection object from pl_connect().

type

Optional flow type to filter by.

name

Optional flow name to filter by.

Value

A data.frame with columns: id, name, type, description, schedule, owner, metadata (list-column), depends_on (list-column of upstream flow names), created, updated.

Examples

## Not run: 
conn <- pl_connect()
pl_get_flows(conn)
pl_get_flows(conn, type = "data_job")
pl_get_flows(conn, name = "ectrl_data_load")

## End(Not run)

Query log entries

Description

Returns a data.frame of log entries, optionally filtered. All filter arguments are optional and combined with AND logic.

Usage

pl_get_logs(
  conn,
  flow = NULL,
  status = NULL,
  from = NULL,
  to = NULL,
  limit = 50
)

Arguments

conn

A connection object from pl_connect().

flow

Optional flow name to filter by.

status

Optional status to filter by ("SUCCESS", "ERROR", or "FATAL").

from

Optional start timestamp (POSIXct or ISO 8601 string).

to

Optional end timestamp (POSIXct or ISO 8601 string).

limit

Maximum number of records to return. Default 50.

Value

A data.frame with columns: id, flow (flow name), log_type, status, message, metadata, logged_by, logged_from (list-column), source_file, source_repo, created.

Examples

## Not run: 
conn <- pl_connect()
pl_get_logs(conn)
pl_get_logs(conn, flow = "ectrl_data_load", status = "ERROR")
pl_get_logs(conn, from = Sys.time() - 86400, limit = 100)

## End(Not run)

Get dependency chain health status for a flow

Description

Walks the DAG upward (recursively through all upstream dependencies) and collects the most recent log entry for each flow in the chain, including the flow itself.

Usage

pl_get_status(conn, flow, since = NULL)

Arguments

conn

A connection object from pl_connect().

flow

Flow name.

since

Optional POSIXct or ISO 8601 string. If provided, only log entries created after this timestamp are considered. Flows with no logs since that time get status = NA.

Value

A data.frame sorted by depth then flow, with columns: flow, type, status, message, created, depth.

Examples

## Not run: 
conn <- pl_connect()
pl_get_status(conn, "ans_monthly_update")
pl_get_status(conn, "ans_monthly_update", since = Sys.time() - 86400)

## End(Not run)

Log an event for a flow

Description

Records a log entry for the named flow. On HTTP failure, retries up to 3 times with 2-second intervals. If all retries fail, emits a warning and returns NULL invisibly — the calling script is never stopped.

Usage

pl_log(
  conn,
  flow,
  status,
  log_type,
  message = NULL,
  metadata = NULL,
  logged_by = NULL,
  logged_from = NULL,
  source_file = NULL,
  source_repo = NULL
)

Arguments

conn

A connection object from pl_connect().

flow

Flow name (character).

status

One of "SUCCESS", "ERROR", or "FATAL".

log_type

Free-text log type (e.g. "data_job", "website_online"). See pl_log_types for standard values, but any string is accepted.

message

Optional human-readable log message.

metadata

Optional named list, serialized to JSON.

logged_by

Optional username of who created this log. If NULL (default), auto-detected from the OS (works on Windows, macOS, and Linux).

logged_from

Optional named list with machine context (e.g. list(machine = "...", os = "...", user = "...")). If NULL (default), auto-detected via Sys.info(). Stored as JSON.

source_file

Optional filename of the script creating this log. If NULL (default), auto-detected from the R call stack or Rscript --file argument. Returns NA in interactive sessions with no source()d file.

source_repo

Optional git repository name where the calling script lives. If NULL (default), auto-detected from the origin remote of the current working directory's git repo.

Value

Invisibly returns the created log record, or NULL on failure.

Examples

## Not run: 
conn <- pl_connect()
pl_log(conn, "ectrl_data_load", "SUCCESS", log_type = "data_job",
       message = "Loaded 14230 rows",
       metadata = list(rows = 14230, duration_s = 45.2))

# Override auto-detected user
pl_log(conn, "ectrl_data_load", "SUCCESS", log_type = "data_job",
       logged_by = "scheduled-task")

## End(Not run)

Default log types

Description

A character vector of the default log type identifiers used by pocketlogR. These are not enforced — any string is accepted as a log type. They exist for documentation, consistency, and convenience.

Usage

pl_log_types

Remove upstream dependencies from a flow

Description

Removes one or more upstream flows from the dependency list of the target flow.

Usage

pl_remove_dependency(conn, flow, depends_on)

Arguments

conn

A connection object from pl_connect().

flow

Name of the flow to update.

depends_on

Character vector of upstream flow names to remove.

Value

Invisibly returns the updated flow record.

Examples

## Not run: 
conn <- pl_connect()
pl_remove_dependency(conn, "ans_data_freshness", "another_upstream_flow")

## End(Not run)

Set up pocketlogR collections in PocketBase

Description

Creates the pl_flows and pl_logs collections with the correct schema and API rules. Requires a superuser connection from pl_connect_admin(). This function is idempotent — safe to call multiple times.

Usage

pl_setup(conn)

Arguments

conn

A superuser connection object from pl_connect_admin().

Value

Invisibly returns NULL.

Examples

## Not run: 
conn_admin <- pl_connect_admin()
pl_setup(conn_admin)

## End(Not run)

Log a SUCCESS event

Description

Convenience wrapper around pl_log() for successful outcomes.

Usage

pl_success(
  conn,
  flow,
  log_type,
  message = NULL,
  metadata = NULL,
  logged_by = NULL,
  logged_from = NULL,
  source_file = NULL,
  source_repo = NULL
)

Arguments

conn

A connection object from pl_connect().

flow

Flow name (character).

log_type

Free-text log type (e.g. "data_job", "website_online"). See pl_log_types for standard values, but any string is accepted.

message

Optional human-readable log message.

metadata

Optional named list, serialized to JSON.

logged_by

Optional username of who created this log. If NULL (default), auto-detected from the OS (works on Windows, macOS, and Linux).

logged_from

Optional named list with machine context (e.g. list(machine = "...", os = "...", user = "...")). If NULL (default), auto-detected via Sys.info(). Stored as JSON.

source_file

Optional filename of the script creating this log. If NULL (default), auto-detected from the R call stack or Rscript --file argument. Returns NA in interactive sessions with no source()d file.

source_repo

Optional git repository name where the calling script lives. If NULL (default), auto-detected from the origin remote of the current working directory's git repo.

Value

Invisibly returns the created log record, or NULL on failure.

Examples

## Not run: 
pl_success(conn, "ectrl_data_load", log_type = "data_job",
           message = "Loaded 14230 rows",
           metadata = list(rows = 14230))

## End(Not run)