@hacksaw/hono-google-cloud-logging
Version:
Google Cloud Logging Middleware for Hono
96 lines (95 loc) • 2.46 kB
TypeScript
/**
* Structure for HTTP request information in log entries
*/
export interface HttpRequestLog {
requestMethod: string;
requestUrl: string;
status: number;
userAgent?: string;
remoteIp?: string;
latency: {
seconds: number;
nanos: number;
};
}
/**
* Structure for log data entries
*/
export interface LogEntryData {
httpRequest: HttpRequestLog;
requestHeaders: Record<string, string>;
responseHeaders: Record<string, string>;
duration: string;
requestBody?: string;
responseBody?: string;
}
export type Severity = "DEFAULT" | "DEBUG" | "INFO" | "NOTICE" | "WARNING" | "ERROR" | "CRITICAL" | "ALERT" | "EMERGENCY";
export interface LoggerOptions {
/**
* Name of the log to write to
*/
name: string;
/**
* Optional resource to associate with the log
*/
resource?: {
type: string;
[key: string]: string;
};
/**
* Optional labels to include with every log entry
*/
labels?: Record<string, string>;
/**
* Project ID for Google Cloud
* If not provided, will be auto-detected from the environment
*/
projectId?: string;
/**
* Whether to log request bodies
* @default false
*/
logRequestBody?: boolean;
/**
* Whether to log response bodies
* @default false
*/
logResponseBody?: boolean;
/**
* Maximum size (in bytes) of request/response bodies to log
* @default 10240 (10KB)
*/
maxBodySize?: number;
/**
* Log severity level
* @default 'DEFAULT'
*/
severity?: Severity;
/**
* Development mode - logs to console instead of Google Cloud Logging
* @default false
*/
dev?: boolean;
/**
* Predicate function to determine if a request should be logged
* Return true to log the request, false to skip logging
* @default () => true (log all requests)
*/
shouldLog?: (context: {
req: Request;
res: Response;
status: number;
duration: number;
error: Error | undefined;
path: string;
method: string;
severity: Severity;
}) => boolean;
}
/**
* Creates a Hono middleware for Google Cloud Logging
*
* @param options Logger configuration options
* @returns Hono middleware handler
*/
export declare const logger: (options?: LoggerOptions) => import("hono/types").MiddlewareHandler<any, string, {}>;