| 1 | import type * as Logs from "@distilled.cloud/aws/cloudwatch-logs"; |
| 2 | import type * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | import type { LogGroup } from "./LogGroup.ts"; |
| 5 | |
| 6 | export type InputLogEvent = Logs.InputLogEvent; |
| 7 | |
| 8 | export interface PutLogEventsRequest extends Omit< |
| 9 | Logs.PutLogEventsRequest, |
| 10 | "logGroupName" | "sequenceToken" |
| 11 | > {} |
| 12 | |
| 13 | /** |
| 14 | * Runtime binding for `logs:PutLogEvents`. |
| 15 | * |
| 16 | * Bind this operation to a `LogGroup` inside a function runtime to get a |
| 17 | * callable that writes log events to a stream of the group (e.g. custom audit |
| 18 | * trails), automatically injecting the log group name. The target log stream |
| 19 | * must already exist (declare an `AWS.Logs.LogStream`). Sequence tokens are no |
| 20 | * longer required by CloudWatch Logs. |
| 21 | * ### Writing Logs |
| 22 | * **Example:** Write an Audit Event |
| 23 | * ```typescript |
| 24 | * const putLogEvents = yield* AWS.Logs.PutLogEvents(logGroup); |
| 25 | * |
| 26 | * yield* putLogEvents({ |
| 27 | * logStreamName: stream.logStreamName, |
| 28 | * logEvents: [{ timestamp: now, message: "user.login id=123" }], |
| 29 | * }); |
| 30 | * ``` |
| 31 | * |
| 32 | * **Example:** Wire into a Lambda Function |
| 33 | * ```typescript |
| 34 | * // Bind in the init phase, call in the handler, and provide the |
| 35 | * // PutLogEventsHttp layer on the Function's init Effect. |
| 36 | * export default AuditFunction.make( |
| 37 | * { main: import.meta.url, functionUrl: true }, |
| 38 | * Effect.gen(function* () { |
| 39 | * const logGroup = yield* AWS.Logs.LogGroup("AuditLogs", { |
| 40 | * retention: "30 days", |
| 41 | * }); |
| 42 | * const stream = yield* AWS.Logs.LogStream("AuditStream", { |
| 43 | * logGroupName: logGroup.logGroupName, |
| 44 | * }); |
| 45 | * const putLogEvents = yield* AWS.Logs.PutLogEvents(logGroup); |
| 46 | * const LogStreamName = yield* stream.logStreamName; |
| 47 | * |
| 48 | * return { |
| 49 | * fetch: Effect.gen(function* () { |
| 50 | * const timestamp = yield* Clock.currentTimeMillis; |
| 51 | * yield* putLogEvents({ |
| 52 | * logStreamName: yield* LogStreamName, |
| 53 | * logEvents: [{ timestamp, message: "audit.event" }], |
| 54 | * }); |
| 55 | * return HttpServerResponse.text("ok"); |
| 56 | * }), |
| 57 | * }; |
| 58 | * }).pipe(Effect.provide(AWS.Logs.PutLogEventsHttp)), |
| 59 | * ); |
| 60 | * ``` |
| 61 | * |
| 62 | * @binding |
| 63 | */ |
| 64 | export interface PutLogEvents extends Binding.Service< |
| 65 | PutLogEvents, |
| 66 | "AWS.Logs.PutLogEvents", |
| 67 | <G extends LogGroup>( |
| 68 | logGroup: G, |
| 69 | ) => Effect.Effect< |
| 70 | ( |
| 71 | request: PutLogEventsRequest, |
| 72 | ) => Effect.Effect<Logs.PutLogEventsResponse, Logs.PutLogEventsError> |
| 73 | > |
| 74 | > {} |
| 75 | export const PutLogEvents = Binding.Service<PutLogEvents>( |
| 76 | "AWS.Logs.PutLogEvents", |
| 77 | ); |