Git Product home page Git Product logo

logger's Introduction

All you need to log with Bref on AWS Lambda.

Build Status Latest Version

Bref/Logger is a lightweight PSR-3 logger for AWS Lambda. Messages are sent to stderr so that they end up in CloudWatch.

Why?

As explained in the Bref documentation, logging in AWS Lambda means logging to stderr. Logs written to stderr are automatically sent to CloudWatch, AWS' solution to collect and view logs.

While classic loggers like Monolog work fine, this logger comes as a simpler and lighter alternative optimized for AWS Lambda. It does not require any configuration and currently contains a single class.

Since it is PSR-3 compliant, Bref/Logger is also compatible with any framework or library consuming a PSR-3 logger.

Installation

composer require bref/logger

Usage

The logger does not require any configuration:

$logger = new \Bref\Logger\StderrLogger();

By default messages above the warning level will be logged, the rest will be discarded.

It is possible to log using any PSR-3 log level, the most common ones being:

$logger->debug('This is a debug message');
$logger->info('This is an info');
$logger->warning('This is a warning');
$logger->error('This is an error');
[WARNING] This is a warning
[ERROR] This is an error

Messages under warning are not logged.

Message placeholders

PSR-3 placeholders can be used to insert information from the $context array into the message without having to concatenate strings manually:

$logger->warning('Invalid login attempt for email {email}', [
    'email' => $email,
]);
[WARNING] Invalid login attempt for email [email protected]

Logging exceptions

Exceptions can be logged under the exception key:

try {
   // ...
} catch (\Exception $e) {
    $logger->error('Impossible to complete the action', [
        'exception' => $e,
    ]);
}
[ERROR] Impossible to complete the action
InvalidArgumentException: Impossible to complete the action in /var/task/index.php:12
Stack trace:
#0 /var/task/index.php(86): main()
...

Log level

It is possible to change the level above which messages are logged.

For example to log all messages:

$logger = new \Bref\Logger\StderrLogger(\Psr\Log\LogLevel::DEBUG);

logger's People

Contributors

mnapoli avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.