Loglayer-Effect provides a seamless integration of loglayer, an intuitive logging library with the Effect ecosystem.
It enables structured, context-aware logging within your Effect applications by providing a LoggingContext service through Effect's Context.Tag.
This allows you to easily manage and configure different logging transports.
const logger = await Effect.runPromise(
Effect.provide(
Effect.gen(function* () {
const logging = yield* LoggingContext;
return yield* logging.logger;
}),
Context.empty().pipe(withStructuredLogging({ prefix: "INFO" })),
),
);To use Loglayer-Effect, you first need to create a logging layer using the withStructuredLogging function.
This function returns a LoggingContext service as an Effect Layer.
You can then provide this layer to your Effect program.
import { Effect, Context } from "effect";
import { LoggingContext, withStructuredLogging } from "@levicape/loglayer-effect";
const loggingLayer = withStructuredLogging({ prefix: "MyAwesomeApp" });
// 2. Create an Effect that uses the logger
const program = Effect.gen(function* () {
const logging = yield* LoggingContext;
const logger = yield* logging.logger;
logger.info("This is an informational message.");
logger.error("This is an error message.", { error: new Error("Something went wrong!") });
});
// 3. Provide the layer to your program
const runnable = Effect.provide(program, loggingLayer);
// 4. Run the program
Effect.runPromise(runnable);withStructuredLogging has the following options:
type LoggingContextOptions = {
readonly prefix: string;
readonly context?: Record<string, unknown>;
};LoggingContext is configured with the following environment variables
LoggingConfigSTRUCTURED_LOGGING: Structured logger to use.pino*consolaAWS Powertools
LOG_LEVEL: Number representing the log level.0is the least verbose,5is the most verbose.CI:LoggingContextwill enable debug logging when this variable is not blank.
LoggingConfigAwsAWS_LAMBDA_FUNCTION_NAME: Lambda handler name. Configured by AWS Lambda environment automatically.LoggingContextconfigures theawspowertoolslogger if set, regardless ofSTRUCTURED_LOGGINGconfiguration.AWS_CLOUDMAP_SERVICE_NAME: AWS Cloud Map service. Enables X-Ray Tracing functionality.
- Enhances logs with the current unix timestamp in the
unixtimeproperty