Coder Social home page Coder Social logo

monolog-factory's Introduction

Monolog Factory

Build Status Code Quality Code Coverage Latest Version PDS Skeleton

Monolog Factory that allows configuration-based creation of Logger objects.

In addition to the generic factory, this package features one to be used with PSR-11 dependency injection containers.

Installation

The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json:

composer require nikolaposa/monolog-factory

Usage

Generic factory

use Monolog\Formatter\HtmlFormatter;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use MonologFactory\LoggerFactory;

$loggerFactory = new LoggerFactory();

$logger = $loggerFactory->create('my_logger', [
    'handlers' => [
        [
            'name' => NativeMailerHandler::class,
            'params' => [
                'to' => '[email protected]',
                'subject' => 'Test',
                'from' => '[email protected]',
                'level' => Logger::ALERT,
            ],
            'formatter' => [
                'name' => HtmlFormatter::class,
            ],
        ],
    ],
    'processors' => [
        [
            'name' => PsrLogMessageProcessor::class,
        ],
    ],
]);

DI container factory configuration

use Monolog\Formatter\HtmlFormatter;
use Monolog\Handler\BufferHandler;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use MonologFactory\DiContainerLoggerFactory;

return [
    'logger' => [
        'logger1' => [
            'name' => 'logger1',
            'handlers' => [
                [
                    'name' => NativeMailerHandler::class,
                    'params' => [
                        'to' => '[email protected]',
                        'subject' => 'Test',
                        'from' => '[email protected]',
                        'level' => Logger::ALERT,
                    ],
                    'formatter' => [
                        'name' => HtmlFormatter::class,
                    ],
                ],
            ],
            'processors' => [
                [
                    'name' => PsrLogMessageProcessor::class,
                ],
            ],
        ],
        'logger2' => [
            'name' => 'logger2',
            'handlers' => [
                [
                    'name' => BufferHandler::class,
                    'params' => [
                        'handler' => [
                            '__class__' => NativeMailerHandler::class,
                            'to' => '[email protected]',
                            'subject' => 'Test',
                            'from' => '[email protected]',
                        ],
                        'buffer_limit' => 5,
                    ],
                    'processors' => [
                        [
                            'name' => MemoryUsageProcessor::class,
                        ],
                    ],
                ],
            ],
            'processors' => [
                [
                    'name' => PsrLogMessageProcessor::class,
                ],
            ],
        ],
    ],
    'di' => [
        'factories' => [
            'Logger1' => new DiContainerLoggerFactory('logger1'),
            //... or more preferred/optimal way:
            'Logger2' => [DiContainerLoggerFactory::class, 'logger2'],
        ],
    ],
];

See more examples.

Credits

License

Released under MIT License - see the License File for details.

monolog-factory's People

Contributors

acelaya avatar nikolaposa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

monolog-factory's Issues

Suggestion about PHP 5 compatibility

Hello

Thanks for this great packages, that is very interresting. I've just one regret, that there is no version compatible with PHP 5.

Perharps a version 1.x compatible PHP5, and get version 2.x compatible PHP 7.

What do you think about this ?

Question: BufferHandler config

How I can describe BufferHandler?

I try:

$loggerFactory = new LoggerFactory();

$logger = $loggerFactory->createLogger('logger', [
    'handlers' => [
        [
            'name' => BufferHandler::class,
            'options' => [
                'handler' => [
                    'name' => NativeMailerHandler::class,
                    'options' => [
                        'to' => '[email protected]',
                        'subject' => 'Test',
                        'from' => '[email protected]',
                        'level' => Logger::ALERT,
                        'formatter' => [
                            'name' => HtmlFormatter::class,
                        ],
                    ],
                ]
            ]
        ]
    ]
]);

composer require psr/container fails

Hey and thx for this lib. May u will bump version of psr/container to ^2.0. On try to install psr/container i get follow problem from composer

Problem 1
    - nikolaposa/monolog-factory[dev-master, 3.1.0] require psr/container ^1.0 -> found psr/container[1.0.0, 1.1.0, 1.1.1, 1.1.x-dev] but it conflicts with your root composer.json require (^2.0).
    - nikolaposa/monolog-factory 3.1.x-dev is an alias of nikolaposa/monolog-factory dev-master and thus requires it to be installed too.
    - Root composer.json requires nikolaposa/monolog-factory ^3.1 -> satisfiable by nikolaposa/monolog-factory[3.1.0, 3.1.x-dev (alias of dev-master)].

thx in advance

PHP 8 support

Hey!

Do you plan to support PHP 8 on this library? I can try to contribute the change if so.

Question: How add a processor for handler

Monolog handler can push custom processor for a handler. Can I define processor for a handler (non all handlers) via this lib.

Example:

'monolog' => [
			'config' => [
                                  'processors' => [...], // global processors for all handlers
				'handlers' => [
					'kibana' => [ ... ],
					'stdout' => [
						'name' => StreamHandler::class,
						'options' => [
							'stream' => 'php://stdout',
						],
                                                  'processors' => [...], // I want add processors for current handler
					],

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.