定制 stackify/log4php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

stackify/log4php

Composer 安装命令:

composer require stackify/log4php

包简介

Stackify logs and errors for Apache log4php

README 文档

README

PHP version

Stackify log4php Appender

Apache log4php appender for sending log messages and exceptions to Stackify. Apache log4php >= 2.2.0 is supported.

Installation

Install the latest version with composer require stackify/log4php

Installation with Linux Agent

This is the suggested installation option, offering the best logging performance.

<root>
    ...
    <appender_ref ref="stackifyAppender" />
</root>

<appender name="stackifyAppender" class="\Stackify\Log\Log4php\Appender">
    <param name="appName" value="application_name" />
    <param name="environmentName" value="environment_name" />
</appender>

Optional Configuration

Log Server Environment Variables

  • Server environment variables can be added to error log message metadata. Note: This will log all system environment variables; do not enable if sensitive information such as passwords or keys are stored this way.
<param name="logServerVariables" value="true" />

Installation without Linux Agent

This option does not require a Stackify Agent to be installed because it sends data directly to Stackify services. It collects log entries in batches, calls curl using the exec function, and sends data to the background immediately [exec('curl ... &')]. This will affect the performance of your application minimally, but it requires permissions to call exec inside the PHP script and it may cause silent data loss in the event of any network issues. This transport method does not work on Windows. To configure ExecTransport you need to pass the environment name and API key (license key):

<root>
    ...
    <appender_ref ref="stackifyAppender" />
</root>

<appender name="stackifyAppender" class="\Stackify\Log\Log4php\Appender">
    <param name="appName" value="application_name" />
    <param name="environmentName" value="environment_name" />
    <param name="mode" value="exec" />
    <param name="apiKey" value="api_key" />
</appender>

Optional Configuration

Proxy

  • ExecTransport supports data delivery through proxy. Specify proxy using libcurl format: <[protocol://][user:password@]proxyhost[:port]>
<param name="proxy" value="https://55.88.22.11:3128" />

Curl path

  • It can be useful to specify curl destination path for ExecTransport. This option is set to 'curl' by default.
<param name="curlPath" value="/usr/bin/curl" />

Notes

To get more error details pass Exception objects to the logger if available:

try {
    $db->connect();
catch (DbException $ex) {
    $logger->error('DB is not available', $ex);
}

All data added to the MDC or NDC will automatically be captured and attached to your log message. This information will be available as JSON data and will be searchable within Stackify.

Troubleshooting

If transport does not work, try looking into vendor\stackify\logger\src\Stackify\debug\log.log file (if it is available for writing). Errors are also written to global PHP error_log. Note that ExecTransport does not produce any errors at all, but you can switch it to debug mode:

<param name="debug" value="1" />

Additional Configuration

For additional configurations, you can set on the XML or the PHP File Configuration. Reference for the additional options are located on the stackify logger repository Stackify PHP Logger - Configuration Settings

XML Configuration

  • The string should be a JSON String and HTML Encoded (will improve this eventually.)
// Sample PHP Config
$config = array(
    'Debug' => true,
    'DebugLogPath' => '/path/to/log.log'
);
// 1. Converting PHP array to JSON String
// - String: {"Debug":true,"DebugLogPath":"/path/to/log.log"}
$jsonString = json_encode($config);
// 2. XML Encode the JSON String
//   - ' is replaced with &apos;
//   - " is replaced with &quot;
//   - & is replaced with &amp;
//   - < is replaced with &lt;
//   - > is replaced with &gt;
// - Encoded String: {&quot;Debug&quot;:true,&quot;DebugLogPath&quot;:&quot;/path/to/log.log&quot;}
// - Note: In case the string has new line ("\n") or a carriage return ("\r") character, you still need to escape it
//   - \n should be replaced with &#10; or &#xA;
//   - \r should be replaced with &#13; or &#xD;
// - Reference: https://stackoverflow.com/a/29924176/14542233
$xmlEncode = htmlspecialchars($jsonString, ENT_XML1 | ENT_QUOTES, 'UTF-8');

// 3. Add it as a value on XML Configuration
<param name="config" value="{&quot;Debug&quot;:true,&quot;DebugLogPath&quot;:&quot;/path/to/log.log&quot;}" />

PHP Configuration

  • If using the PHP configuration, basically add the config attribute on the params under the Stackify Appender settings
<?php
return array(
    'rootLogger' => array(
        'appenders' => array('stackifyAppender'),
    ),
    'appenders' => array(
        'stackifyAppender' => array(
            'class' => '\Stackify\Log\Log4php\Appender',
            'params' => array(
                'appName' => 'application-name',
                'environmentName' => 'environment-name',
                // Additional configuration
                'config' => array(
                    'Debug' => true,
                    'DebugLogPath' => 'logConfig.log'
                )
            )
        )

License

Copyright 2019 Stackify, LLC.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

stackify/log4php 适用场景与选型建议

stackify/log4php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 654 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 12 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 stackify/log4php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 654
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 13
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2014-12-29