Skip to content

Latest commit

 

History

History

README.md

Logging

A sample app that demonstrates how to use Juno's built-in logging functions in Rust serverless functions.

Instead of ic_cdk::print (which shows as "Unknown" level in the Juno Console), this example uses junobuild_satellite::{info, debug, warn, error} to emit structured log entries with proper levels.

Key Files

Getting started

git clone https://github.com/junobuild/examples
cd functions/rust/logging

How to Run

  1. Install dependencies:
npm ci
  1. Start Juno local emulator:

Requires the Juno CLI: npm i -g @junobuild/cli

juno emulator start
  1. Create a Satellite for local dev:
  1. Create required collection:
  1. Start the frontend dev server (in a separate terminal):
npm run dev
  1. Build the serverless functions (in a separate terminal):
juno functions build

The emulator will automatically upgrade your Satellite and live reload the changes.

  1. Test logging:
  • Sign in and save a note using the frontend.
  • Check the Juno Console → Functions → Logs tab to see log entries with proper levels.
  • Save a note with an empty body to trigger a warning-level log.

How It Works

Log Levels

Function Level Use Case
junobuild_satellite::debug() Debug Diagnostic messages for development
junobuild_satellite::info() Info Successful operations, state changes
junobuild_satellite::warn() Warning Skipped operations, unusual conditions
junobuild_satellite::error() Error Failed operations, unexpected errors

RNG Dependency

Custom loggers require a seeded RNG to generate unique document keys. Enable the on_init_random_seed feature in your Cargo.toml:

junobuild-satellite = { version = "0.2.0", features = ["on_set_doc", "on_init_random_seed"] }

Then implement the callback to know when loggers are ready:

#[unsafe(no_mangle)]
fn juno_on_init_random_seed() {
    // Custom loggers are now available
    logging::log_info("Satellite", "RNG seeded — loggers ready");
}

Why Not ic_cdk::print?

ic_cdk::print junobuild_satellite::info() etc.
Juno Console Level "Unknown" Info / Debug / Warning / Error
Availability Always After RNG seed
Best for Quick debug, pre-seed fallback Production logging

The logging.rs module in this example wraps both: it tries the Juno logger first and falls back to ic_cdk::print if it fails (e.g. before the RNG is seeded).