承接 bref/monolog-bridge 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bref/monolog-bridge

Composer 安装命令:

composer require bref/monolog-bridge

包简介

Monolog formatter optimized for AWS Lambda and CloudWatch

README 文档

README

This repository provides a Monolog log formatter optimized for AWS Lambda and CloudWatch.

Read all about logs in the Bref documentation.

Logs will be formatted to combine readability and structured data, making it easy to both read logs and filter them. Here's an example of a log record:

INFO   This is a log message   {"message":"This is a log message","level":"INFO"}

The JSON object contains the following fields (when applicable):

{
    "message": "Uncaught exception: 'RuntimeException' with message 'Exception message'",
    "level": "ERROR",
    "exception": {
        "class": "RuntimeException",
        "message": "Exception message",
        "code": 0,
        "file": "/var/task/index.php:11",
        "trace": [ ... ]
    },
    "context": {
        "key": "value",
        ...
    },
    "extra": {
        "key": "value",
        ...
    }
}

The JSON object is parsed by CloudWatch, allowing you to filter logs in CloudWatch Logs Insights. For example:

fields @timestamp, @message
| filter level = 'ERROR'
| filter exception.class = 'RuntimeException'
| sort @timestamp desc
| limit 1000

Installation

Important

This package is already installed if you use Bref's Laravel bridge or Symfony bridge. If you do, you don't need to install this package.

composer require bref/monolog-bridge

Usage

For Laravel applications, set the following environment variable in serverless.yml:

provider:
    environment:
        LOG_STDERR_FORMATTER: Bref\Monolog\CloudWatchFormatter

For Symfony applications, set the Bref Monolog formatter in your config/packages/prod/monolog.yaml:

monolog:
    handlers:
        file:
            type: stream
            level: info
            formatter: 'Bref\Monolog\CloudWatchFormatter'

For other applications, you can set the formatter in your Monolog configuration. For example:

$handler = new Monolog\Handler\StreamHandler('php://stderr', Monolog\Logger::INFO);
$handler->setFormatter(new Bref\Monolog\CloudWatchFormatter);
$logger = new Monolog\Logger('default');
$logger->pushHandler($handler);

Motivation

The goal of this is to improve the overall logs experience with CloudWatch.

The usual format is to log unstructured text. This sucks because:

  • we can't filter by log level (e.g. look for errors)
  • we can't easily filter and see all logs of a request/invocation via the AWS request ID (e.g. as log metadata)
  • some log messages are split across multiple lines
  • exception traces especially are split across loads of lines, not easy to navigate in CloudWatch or other line-based viewers

Here's an example:

Screen-002902

A better approach is to switch to JSON-formatted logs:

  • we can add metadata to log records (e.g. request ID to filter/see all logs of a request, or filter by log level)
  • exception traces are organized as sub-metadata of the exception record, keeping things easier to navigate

But this isn't perfect:

  • depending on the tool you use to view the logs, the UX can be very very poor
  • it's hard to explore the logs quickly because now all info is nested in a log record, with no hierarchy to preview "just the message" quickly
  • Monolog's JSON formatter adds tons of info to the JSON record

Here's an example (Bref Cloud formats the JSON payload FYI, this isn't the case in CloudWatch):

Screen-002903

CloudWatch has some great features though:

This is the log message {"key":"value"}

The log message above will automatically get parsed as text (the first part) and a JSON object attached (the second part).

That means we can have a simple log message + a structured JSON object.

On top of that, with CloudWatch:

  • we don't need to log the timestamp, CloudWatch automatically timestamps log records
  • I don't think we need to log the "channel" name: IMO channels make sense to split web logs from jobs or artisan commands, but in Lambda these are separate functions/logs anyway, so I think we can remove that part too

This is why I want to add this CloudWatchFormatter optimized for Bref users.

As you can see in the screenshots below, it is easy to navigate the logs textually. But it's also possible to have a lot more information nested in the log records via the JSON object.

In CloudWatch:

Screen-002905

In Bref Cloud:

Screen-002904

In CloudWatch Logs Insights, we can see that keys of the JSON objects are correctly detected:

Screen-002906

That allows us to create advanced queries, for example to search for "ERROR" logs with a specific exception class:

Screen-002907

bref/monolog-bridge 适用场景与选型建议

bref/monolog-bridge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.7M 次下载、GitHub Stars 达 11, 最近一次更新时间为 2025 年 06 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「monolog」 「aws」 「lambda」 「cloudwatch」 「bref」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bref/monolog-bridge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bref/monolog-bridge 我们能提供哪些服务?
定制开发 / 二次开发

基于 bref/monolog-bridge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.7M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 31
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-14