/** * The Stacks adapter: one call, and the app's own log stream ships to loghq. * * A Stacks application already logs. It has `log.info` / `log.error` at every * interesting point and a `log.struct` namespace for the framework's own * events. The job of this file is to drain that stream without asking anyone to * rewrite a single call site, which is why the whole surface is `install()`. * * Two seams exist, probed in preference order at runtime: * * 1. A transport registry on `@stacksjs/logging`. This is where a logger is * supposed to be extended. It landed upstream as `registerTransport()` * plus a `transports` key on `config/logging.ts`, so a modern framework * version takes this branch. See {@link tryTransport}, and * {@link loghqTransport} for the declarative form. * 2. Teeing the mutable `log` singleton. Stacks exports `log` as a plain * object whose methods are reassignable, and `log.struct` routes back * through those same methods, so wrapping six functions captures both the * ad-hoc and the structured stream. This is the fallback on a framework * version that predates the registry. * * The recursion hazard is the thing to understand before changing anything * here. We are wrapping the very logger the SDK would reach for if it ever * reported a problem, so a transport failure logged through `log.error` would * arrive back at the wrapper that produced it. Every diagnostic in this file * goes to `console` directly, and {@link forward} holds a module-level * re-entrancy flag so a fault inside the forward path cannot start a second * one. */ import type { DisabledReason, LogHQConfig, StacksLogTransport } from './types'; import { LogHQClient } from './client'; /** Options the adapter adds on top of {@link LogHQConfig}. */ export interface StacksOptions { /** * Install process-level handlers for uncaught exceptions and unhandled * rejections. Default `true`. * * A crash never reaches `log.*`, so the last and most interesting thing the * process did would otherwise be the one thing missing from the stream. */ captureUnhandled?: boolean; /** * Forward `log.struct` events: `http.request`, `db.query`, `db.slow_query`, * `job.*`, `cache.*`. Default `true`. * * Worth turning off once the framework emits these per request and the volume * costs more quota than the visibility is worth. */ captureStruct?: boolean; /** * The Stacks `log` singleton, or the whole `@stacksjs/logging` namespace, * handed over explicitly. * * Skips the runtime probe, which is the only way to attach synchronously and * the seam tests use to attach to a double. Applications should leave it * unset and let `install()` find the real one. */ logger?: unknown; } /** Options {@link loghqTransport} adds on top of {@link LogHQConfig}. */ export interface TransportOptions { /** * The transport's name in the framework's diagnostics. Default `loghq`. * * Worth changing only when an app declares two of these, e.g. one project for * application logs and another for audit. */ name?: string; /** Forward `log.struct` events. Default `true`. See {@link StacksOptions}. */ captureStruct?: boolean; } /** * Which attachment point the adapter ended up using. * * `conflict` means another copy of this package in the same process already * owns the logger, so this installation is inert. It is reported separately * from `none` because the fix is completely different: `none` means no logger * was found, `conflict` means one was found and someone else got there first. */ export type LoggerSeam = 'transport' | 'tee' | 'none' | 'conflict'; export interface SeamInfo { seam: LoggerSeam; /** The exact entry point taken, e.g. `registerTransport` or `log.*`. Null when unattached. */ via: string | null; } /** * A seam, plus whether anything will actually come out of it. * * Attachment and delivery are separate failures and only the first is visible * from the outside. A transport with no ingest key attaches perfectly: the * logger holds it, it is called for every record, and it drops all of them, * because the client disabled itself on construction. Reporting that as * attached is true and useless — it is exactly the state an app sits in when * `LOGHQ_KEY` never reached the box. */ export interface AttachmentInfo extends SeamInfo { /** True when the client behind this seam will send. */ live: boolean; /** * Why the client stopped, when it diagnosed a failure — `auth` for a missing * or rejected key. Null both when nothing is wrong and when the client is * merely configured `enabled: false`, so check {@link live} first. */ disabledReason: DisabledReason | null; /** * Whether {@link live} was measured or assumed. * * False when the logger holds a matching transport this copy of the package * did not build — another copy did, and its client is not reachable from * here. `live` is then optimistic. Assert on this too if you would rather a * boot check be strict than forgiving. */ introspected: boolean; } /** * Start streaming this Stacks app's logs to loghq. * * Returns immediately with a live client. When the logger has to be discovered * by dynamic import the attachment lands a microtask later, so lines emitted in * the same tick as `install()` can be missed. Pass `logger` or await * {@link whenAttached} if that matters. */ export declare function install(config: LogHQConfig & StacksOptions): LogHQClient; /** * Detach from the logger and the process, restoring what was there before. * * Leaves the client alive and holding its queue, because a caller who wants the * queued entries gone should say so by closing the client. */ export declare function uninstall(): void; /** Which seam is live right now. Cheap, synchronous, and safe to log. */ export declare function activeSeam(): SeamInfo; /** Resolves once the attach attempt started by {@link install} has settled. */ export declare function whenAttached(): Promise; /** The client the current installation owns, if any. */ export declare function installedClient(): LogHQClient | null; /** * Whether loghq is really attached — asked of the logger, not inferred. * * {@link activeSeam} only sees installations {@link install} created, because a * transport declared in `config/logging.ts` is owned by that config and * deliberately never becomes this module's `current` installation. So on the * seam most apps ship — the declarative one — `activeSeam()` answers `none`, * which reads as failure and is not one. * * Nor does constructing the transport prove anything: a config file is * evaluated whether or not the framework goes on to read `transports`. On a * framework version without transport support, the config loads, this package * loads, `loghqTransport()` returns a perfectly good transport, and not one * record is ever delivered. Since 0.2.3 carries no `peerDependencies`, nothing * upstream of this warns about it either. * * Asking the logger which transports it actually holds is the only question * whose answer separates those two cases, so it is the one worth putting in a * boot assertion. * * @param options.name Transport name to look for. Match the `name` given to * {@link loghqTransport}, which defaults to `loghq`. * @param options.logger The logger to interrogate, skipping the runtime probe * for `@stacksjs/logging`. The same injection point {@link install} takes, and * the only way to ask the question of something that is not a real Stacks app. */ export declare function verifyAttached(options?: { name?: string; logger?: unknown; }): Promise; /** * A loghq transport for `config/logging.ts`. * * The declarative half of this package, and the better half where the * framework supports it. `install()` has to find a seam at runtime and patch * it; this is handed the stream by the logger itself, which means no patching, * no ordering problem, and no per-process install: every process that boots the * framework reads the same config. * * @example * ```ts * // config/logging.ts * import { loghqTransport } from '@loghq/stacks' * * export default { * logsPath: storagePath('logs/stacks.log'), * deploymentsPath: storagePath('logs/deployments.log'), * transports: [loghqTransport({ key: env.LOGHQ_KEY })], * } satisfies LoggingConfig * ``` * * Crash reports arrive without any extra wiring: the framework's own * `uncaughtException` and `unhandledRejection` handlers funnel through * `report()`, which calls `log.error`, which reaches this transport like any * other line. That is why this does not install process handlers of its own the * way `install()` does. A bare script with no framework entry point still wants * `install()`. * * Returns a plain object, so a config file stays a config file: nothing is * patched and nothing is registered until the logger reads it. */ export declare function loghqTransport(config?: LogHQConfig & TransportOptions): StacksLogTransport;