Skip to content

Commit

Permalink
release 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vrielsa committed Jun 26, 2020
0 parents commit 92ea161
Show file tree
Hide file tree
Showing 14 changed files with 1,041 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/grumphp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
on: [push]

jobs:
build:
name: Checking coding standards
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache Composer Downloads
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Cache PHP dependencies
uses: actions/cache@v1
with:
path: vendor
key: ${{ runner.OS }}-build-${{ hashFiles('**/composer.lock') }}

- uses: MilesChou/composer-action@master
with:
args: config "http-basic.repo.magento.com" ${{ secrets.MAGE_USER }} ${{ secrets.MAGE_PASS }}

- uses: MilesChou/composer-action@master
with:
args: install --prefer-dist

- run: ./vendor/bin/grumphp run
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/composer.lock
/vendor
/.idea
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## [2.0.0]
### Added
- Custom logger handler which is configurable
- Interface for logger handler configuration
- Source model with a list of Log Levels
11 changes: 11 additions & 0 deletions Config/LogConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types = 1);

namespace Phpro\LoggerHandler\Config;

interface LogConfiguration
{
public function getLogFileName(): string;

public function getLogLevel(): string;
}
48 changes: 48 additions & 0 deletions Config/LogLevelsSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types = 1);

namespace Phpro\LoggerHandler\Config;

use Magento\Framework\Option\ArrayInterface;
use Psr\Log\LogLevel;

class LogLevelsSource implements ArrayInterface
{
public function toOptionArray(): array
{
return [
[
'value' => LogLevel::EMERGENCY,
'label' => LogLevel::EMERGENCY
],
[
'value' => LogLevel::ALERT,
'label' => LogLevel::ALERT
],
[
'value' => LogLevel::CRITICAL,
'label' => LogLevel::CRITICAL
],
[
'value' => LogLevel::ERROR,
'label' => LogLevel::ERROR
],
[
'value' => LogLevel::WARNING,
'label' => LogLevel::WARNING
],
[
'value' => LogLevel::NOTICE,
'label' => LogLevel::NOTICE
],
[
'value' => LogLevel::INFO,
'label' => LogLevel::INFO
],
[
'value' => LogLevel::DEBUG,
'label' => LogLevel::DEBUG
]
];
}
}
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015-2020 Phpro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 changes: 26 additions & 0 deletions Logger/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Phpro\LoggerHandler\Logger;

use Magento\Framework\Filesystem\DriverInterface;
use Magento\Framework\Logger\Handler\Base;
use Monolog\Processor\PsrLogMessageProcessor;
use Phpro\LoggerHandler\Config\LogConfiguration;

/**
* Override Base Logger Handler to make log file name and log level configurable
*/
class Handler extends Base
{
public function __construct(
LogConfiguration $config,
DriverInterface $filesystem,
string $filePath = null
) {
$fileName = $config->getLogFileName();
$this->loggerType = $config->getLogLevel();
$this->pushProcessor(new PsrLogMessageProcessor()); // @codingStandardsIgnoreLine

parent::__construct($filesystem, $filePath, $fileName);
}
}
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
![](https://github.com/phpro/phpro-mage2-module-logger-handler/workflows/.github/workflows/grumphp.yml/badge.svg)

# Logger Handler for Magento 2

This module allows you to easily configure custom log files. Especially useful when building several integrations, each requiring a separate log file.

## Installation

`composer require phpro/mage2-module-logger-handler`

## How to use

This module is only a basic building block. You can build on top of this to create custom log files in your projects.
Below you can find an example implementation.

### Stores configuration

To use a custom log file, you can add the following to the system config. This module also provides a source model for the log levels.

<!-- system.xml -->
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="module" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<group id="log_type" translate="label" sortOrder="20" showInDefault="1">
<field id="log_file_name" translate="label" type="text" sortOrder="10" showInDefault="1">
<label>Log File Name</label>
</field>
<field id="log_level" translate="label" type="select" sortOrder="20" showInDefault="1">
<label>Log Level</label>
<source_model>Phpro\LoggerHandler\Config\LogLevelsSource</source_model> <!-- Custom source model which is available in this module -->
</field>
</group>
</section>
</system>
</config>


### Configuration class

You will need to create a Configuration class which implements the LogConfiguration interface. This Configuration class will be used by the Logger Handler.

This is an example of a Configuration class which uses the stores configuration defined above.

<?php

namespace Vendor\Module\Config;

use Phpro\LoggerHandler\Config\LogConfiguration;
use Magento\Framework\App\Config\ScopeConfigInterface;

class SystemConfiguration implements LogConfiguration
{
const XML_LOG_FILE_NAME = 'module/log_type/log_file_name';
const XML_LOG_LEVEL = 'module/log_type/log_level';
const LOG_DIR = 'var' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;

/**
* @var ScopeConfigInterface
*/
private $config;

public function __construct(ScopeConfigInterface $config)
{
$this->config = $config;
}

/**
* This function should return the full path to the log file starting from the magento root
*/
public function getLogFileName(): string
{
return self::LOG_DIR . $this->config->getValue(self::XML_LOG_FILE_NAME);
}

public function getLogLevel(): string
{
return $this->config->getValue(self::XML_LOG_LEVEL);
}
}


### Virtual Types

You will need to create the following Virtual Types to use a custom logger in a service class.

<!-- di.xml -->
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="[vendor_module_logtype]_logger_handler" type="Phpro\LoggerHandler\Logger\Handler">
<arguments>
<argument name="config" xsi:type="object">Vendor\Module\Config\SystemConfiguration</argument> <!-- Configuration class created above -->
</arguments>
</virtualType>
<virtualType name="[vendor_module_logtype]_logger" type="Monolog\Logger">
<arguments>
<argument name="name" xsi:type="string">[module-logtype]-logger</argument> <!-- channel name; will also show in log files -->
<argument name="handlers" xsi:type="array">
<item name="stream" xsi:type="object">[vendor_module_logtype]_logger_handler</item> <!-- refers to the logger handler VirtualType -->
</argument>
</arguments>
</virtualType>
<!-- inject custom logger in service class -->
<type name="Vendor\Module\Service\DoSomething">
<arguments>
<argument name="logger" xsi:type="object">[vendor_module_logtype]_logger</argument> <!-- refers to the logger VirtualType -->
</arguments>
</type>
</config>
13 changes: 13 additions & 0 deletions bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
image: php:7.1-cli

pipelines:
default:
- step:
script:
- apt-get update && apt-get install -y git unzip libxml2-dev
- echo "memory_limit=512M" >> /usr/local/etc/php/php.ini
- php -r "readfile('https://getcomposer.org/installer');" | php
- php composer.phar config http-basic.repo.magento.com $MAGE_USER $MAGE_PASS
- php composer.phar global require hirak/prestissimo
- php composer.phar install --prefer-dist
- ./vendor/bin/grumphp run
51 changes: 51 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "phpro/mage2-module-logger-handler",
"description": "This module allows you to easily configure custom log files",
"type": "magento2-module",
"license": "MIT",
"authors": [
{
"name": "PHPro NV",
"email": "[email protected]",
"homepage": "https://www.phpro.be/"
}
],
"require": {
"php": "^7.0",
"magento/framework": "^100.1.7|^101.0.1|^102.0"
},
"require-dev": {
"magento/marketplace-eqp": "^1.0",
"phpro/grumphp": "^0.14"
},
"autoload": {
"psr-4": {
"Phpro\\LoggerHandler\\": ""
},
"files": [
"registration.php"
]
},
"repositories": [
{
"type": "composer",
"url": "https://repo.magento.com/"
},
{
"type": "git",
"url": "https://github.com/magento/marketplace-eqp"
}
],
"config": {
"platform": {
"ext-gd": "7.1",
"ext-mcrypt": "7.1",
"ext-itl": "7.1",
"ext-xsl": "7.1",
"ext-bcmath": "7.1",
"ext-pdo_mysql": "7.1",
"ext-soap": "7.1",
"ext-zip": "7.1"
}
}
}
5 changes: 5 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Phpro_LoggerHandler" setup_version="1.0.0" />
</config>
17 changes: 17 additions & 0 deletions grumphp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
parameters:
tasks:
phpcs:
standard: "ruleset.xml"
warning_severity: 0
ignore_patterns:
- "Test/Unit"
triggered_by: [php]
git_blacklist:
keywords:
- "die("
- "var_dump("
- "exit;"
- "console.log("
triggered_by: [php,js]
file_size:
max_size: 1M
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Phpro_LoggerHandler',
__DIR__
);
Loading

0 comments on commit 92ea161

Please sign in to comment.