# fullstack-build/tslog architecture

> Architecture diagram of the GitHub repository fullstack-build/tslog, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fullstack-build/tslog
- Repository: https://github.com/fullstack-build/tslog

## Overview

##### Project Type and Purpose
- This is an **open-source logging library** (`tslog`) for TypeScript and JavaScript.
- It provides a **universal** API (`Logger`, `BaseLogger`) that runs in both **Node.js** and **browser** environments, outputs “pretty” or JSON logs, supports masking, custom transports, custom log levels, and sub-loggers with inheritance.
- Primary consumers are other applications or libraries that import `tslog` from **npm**.

##### High-Level File Structure Analysis
- **src/** – Core TypeScript source code:
- `BaseLogger.ts` – Implements log-lifecycle steps (mask, toLogObj, addMeta, format, transport)
- `Logger` entry points: `index.ts`, `index.browser.ts`
- `runtime/browser/` & `runtime/nodejs/` – Environment-specific helpers (e.g. `util.inspect.polyfill`)
- Formatters, style definitions, and interfaces
- **build.js**, **tsconfig\*.json** – Build and packaging configuration for CJS and ESM outputs
- **package.json**, **npmignore**, **.npmrc** – NPM packaging and publishing settings
- **docs/** – Static site for API documentation and demo screenshots
- **examples/** – Live sample projects (browser, Node.js CJS/ESM, SvelteKit) showing integration
- **tests/** – Jest/Puppeteer test suites verifying browser and Node.js behavior
- **.github/** – CI workflow (`ci.yml`), code coverage, issue templates

##### Key Architecture Insights from README
- **Lifecycle of a log message** with five pluggable steps:
1. `mask`
2. `toLogObj`
3. `addMetaToLogObj`
4. `format` (pretty vs JSON)
5. `transport` (console, custom)
- **Strategy/Plugin pattern** for transports and formatters
- Support for **CJS & ESM** bundling with tree-shaking
- **Environment adapters** for browser vs Node.js specifics
- **Extensibility points**: override any step, attach transports, custom log object, custom levels

##### Main Components
a. **Source Modules**
- Core API (`Logger`, `BaseLogger`)
- Format & Style Engines (`prettyFormatLogObj`, templates, `prettyLogStyles`)
- Environment Runtimes (Browser adapter, Node.js adapter)
- Settings & Interfaces (`ISettings`, `ILogObj`, hooks for override)
b. **Build & Packaging**
- TypeScript compiler (`tsconfig.cjs.json`, `tsconfig.esm.json`)
- Bundler script (`build.js`)
- Outputs: CJS (`package-cjs.json`), ESM (`src/index.ts`)
- NPM registry (publish process)
c. **Testing & CI**
- Jest presets (`jest-preset.cjs`) and Puppeteer config
- Test directories for Node and Browser
- GitHub Actions workflow
d. **Documentation & Examples**
- Docs site generation (`docs/` folder)
- Sample consumer projects under `examples/`

##### Relationships and Data Flow
- **Developer** writes/imports code → **tslog API** → calls **BaseLogger.log()**
- **Logger.log()** triggers the **lifecycle pipeline** (mask → toLogObj → addMeta → format → transport)
- **Transport plugins** send output to **console**, **attached transports**, or **external services**
- **Build process** compiles `src/` into CJS and ESM bundles, runs **tests**, publishes to **npm**
- **Docs generator** picks up markdown and assets from `docs/` → publishes to GitHub Pages
- **Examples** illustrate integration in real consumer environments

##### Architectural Patterns and Design Principles
- **Strategy Pattern** for transports and formatting strategies
- **Template Method Pattern** in the log lifecycle with overridable hooks
- **Adapter Pattern** for browser vs Node.js runtimes
- **Builder/Fluent API** for configuring logger settings and sub-loggers
- **Dependency Injection** of settings, transports, and default log objects

##### Essential Detail
- Lifecycle step and where overrides/injections happen.
- Distinguish between **CJS** and **ESM** outputs.
- Both **browser** and **Node.js** runtime flows.
- External integration points: console, file system, third-party transports (Slack, rotating file).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Library
    subgraph "Core Library"
        direction TB
        BaseLogger["BaseLogger"]:::code
        LoggerESM["Logger (ESM)"]:::code
        LoggerBrowser["Logger (Browser)"]:::code
        Interfaces["ISettings & Interfaces"]:::code
    end

    %% Format & Style Engines
    subgraph "Format & Style Engines"
        direction TB
        TemplateFormatter["prettyFormatLogObj"]:::code
        NumberFormatter["formatNumberAddZeros"]:::code
        Styles["prettyLogStyles"]:::code
    end

    %% Environment Runtimes
    subgraph "Environment Runtimes"
        direction TB
        BrowserAdapter["Browser Adapter"]:::code
        NodeAdapter["Node.js Adapter"]:::code
        subgraph "Browser Helpers"
            direction TB
            JSONHelper["helper.jsonStringifyRecursive"]:::code
            InspectPolyfill["util.inspect.polyfil"]:::code
            InspectOptions["InspectOptions.interface"]:::code
        end
    end

    %% Lifecycle Pipeline
    subgraph "Log Lifecycle Pipeline"
        direction TB
        Mask["mask()"]:::code
        ToObj["toLogObj()"]:::code
        AddMeta["addMetaToLogObj()"]:::code
        FormatStep["format()"]:::code
        TransportStep["transport()"]:::code
    end

    %% Transports
    subgraph "Transport Plugins"
        direction TB
        Console["Console"]:::ext
        Custom["Custom Transport"]:::ext
        External["External Services"]:::ext
    end

    %% Build & Packaging
    subgraph "Build & Packaging"
        direction TB
        BuildScript["build.js"]:::build
        TSConfigAll["tsconfig*.json"]:::build
        CJSManifest["package-cjs.json"]:::build
    end

    %% Testing & CI
    subgraph "Testing & CI"
        direction TB
        JestPreset["jest-preset.cjs"]:::test
        PuppeteerCfg["jest-puppeteer.config.cjs"]:::test
        NodeTests["tests/Nodejs/"]:::test
        BrowserTests["tests/Browser/"]:::test
        CIWorkflow[".github/workflows/ci.yml"]:::test
    end

    %% Docs & Examples
    subgraph "Documentation & Examples"
        direction TB
        Docs["docs/"]:::docs
        ExamplesBrowser["examples/browser/"]:::docs
        ExamplesNode["examples/nodejs/"]:::docs
        ExamplesSvelte["examples/sveltekit_vite/"]:::docs
    end

    %% Consumer & Publish
    subgraph "Publish & Consumers"
        direction TB
        Compiler["TypeScript Compiler"]:::build
        BundlesCJS["CJS Bundle"]:::build
        BundlesESM["ESM Bundle"]:::build
        NPM["npm Registry"]:::ext
        Consumers["Consumer Applications"]:::ext
    end

    %% Developer to API
    Developer["Developer"]:::ext -->|"imports tslog API"| LoggerESM
    Developer -->|"imports tslog API"| LoggerBrowser

    %% API to BaseLogger
    LoggerESM -->|"extends"| BaseLogger
    LoggerBrowser -->|"extends"| BaseLogger

    %% Logger.log to Pipeline
    BaseLogger -->|"calls log()"| Mask
    Mask --> ToObj
    ToObj --> AddMeta
    AddMeta --> FormatStep
    FormatStep --> TransportStep

    %% Transport outputs
    TransportStep --> Console
    TransportStep --> Custom
    TransportStep --> External

    %% Format & Style usage
    FormatStep --> TemplateFormatter
    FormatStep --> NumberFormatter
    FormatStep --> Styles

    %% Environment adapters usage
    BaseLogger --> BrowserAdapter
    BaseLogger --> NodeAdapter
    BrowserAdapter --> JSONHelper
    BrowserAdapter --> InspectPolyfill
    BrowserAdapter --> InspectOptions

    %% Build flow
    BaseLogger & LoggerESM & LoggerBrowser & TemplateFormatter & NumberFormatter & Styles & Interfaces & BrowserAdapter & NodeAdapter -->|"compile"| Compiler
    Compiler --> BundlesCJS
    Compiler --> BundlesESM
    BundlesCJS --> CJSManifest
    BundlesESM --> CJSManifest
    BundlesCJS & BundlesESM -->|"publish"| NPM
    NPM --> Consumers

    %% Testing & CI flow
    CIWorkflow --> JestPreset
    CIWorkflow --> PuppeteerCfg
    CIWorkflow --> NodeTests
    CIWorkflow --> BrowserTests

    %% Docs & Examples flow
    Docs -->|"site generation"| ExamplesBrowser
    Docs --> ExamplesNode
    Docs --> ExamplesSvelte

    %% Sub-logger inheritance
    BaseLogger -.->|"create sub-logger"| BaseLogger

    %% Click Events
    click BaseLogger "https://github.com/fullstack-build/tslog/blob/master/src/BaseLogger.ts"
    click LoggerESM "https://github.com/fullstack-build/tslog/blob/master/src/index.ts"
    click LoggerBrowser "https://github.com/fullstack-build/tslog/blob/master/src/index.browser.ts"
    click TemplateFormatter "https://github.com/fullstack-build/tslog/blob/master/src/formatTemplate.ts"
    click NumberFormatter "https://github.com/fullstack-build/tslog/blob/master/src/formatNumberAddZeros.ts"
    click Styles "https://github.com/fullstack-build/tslog/blob/master/src/prettyLogStyles.ts"
    click BrowserAdapter "https://github.com/fullstack-build/tslog/blob/master/src/runtime/browser/index.ts"
    click JSONHelper "https://github.com/fullstack-build/tslog/blob/master/src/runtime/browser/helper.jsonStringifyRecursive.ts"
    click InspectPolyfill "https://github.com/fullstack-build/tslog/blob/master/src/runtime/browser/util.inspect.polyfil.ts"
    click InspectOptions "https://github.com/fullstack-build/tslog/blob/master/src/runtime/browser/InspectOptions.interface.ts"
    click NodeAdapter "https://github.com/fullstack-build/tslog/blob/master/src/runtime/nodejs/index.ts"
    click Interfaces "https://github.com/fullstack-build/tslog/blob/master/src/interfaces.ts"
    click BuildScript "https://github.com/fullstack-build/tslog/blob/master/build.js"
    click CJSManifest "https://github.com/fullstack-build/tslog/blob/master/src/package-cjs.json"
    click TSConfigAll "https://github.com/fullstack-build/tslog/blob/master/tsconfig.json"
    click TSConfigAll "https://github.com/fullstack-build/tslog/blob/master/tsconfig.cjs.json"
    click TSConfigAll "https://github.com/fullstack-build/tslog/blob/master/tsconfig.esm.json"
    click TSConfigAll "https://github.com/fullstack-build/tslog/blob/master/tsconfig.types.json"
    click TSConfigAll "https://github.com/fullstack-build/tslog/blob/master/tsconfig.example.json"
    click JestPreset "https://github.com/fullstack-build/tslog/blob/master/jest-preset.cjs"
    click PuppeteerCfg "https://github.com/fullstack-build/tslog/blob/master/jest-puppeteer.config.cjs"
    click NodeTests "https://github.com/fullstack-build/tslog/tree/master/tests/Nodejs/"
    click BrowserTests "https://github.com/fullstack-build/tslog/tree/master/tests/Browser/"
    click CIWorkflow "https://github.com/fullstack-build/tslog/blob/master/.github/workflows/ci.yml"
    click Docs "https://github.com/fullstack-build/tslog/tree/master/docs/"
    click ExamplesBrowser "https://github.com/fullstack-build/tslog/tree/master/examples/browser/"
    click ExamplesNode "https://github.com/fullstack-build/tslog/tree/master/examples/nodejs/"
    click ExamplesSvelte "https://github.com/fullstack-build/tslog/tree/master/examples/sveltekit_vite/"

    %% Styles
    classDef code fill:#E3F2FD,stroke:#90CAF9,stroke-width:1px
    classDef build fill:#E8F5E9,stroke:#A5D6A7,stroke-width:1px
    classDef test fill:#ECEFF1,stroke:#B0BEC5,stroke-width:1px
    classDef docs fill:#FFF3E0,stroke:#FFCC80,stroke-width:1px
    classDef ext fill:#F3E5F5,stroke:#CE93D8,stroke-width:1px
```
