Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

[*.yml]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
indent_style = space
43 changes: 43 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Basic set up for three package managers

version: 2
updates:

# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
# Workflow files stored in the default location of `.github/workflows`. (You don't need to specify `/.github/workflows` for `directory`. You can use `directory: "/"`.)
directory: "/"
schedule:
interval: "weekly"
groups:
actions:
patterns:
- "*"
commit-message:
prefix: "(chore)"

# Maintain dependencies for npm
# - package-ecosystem: "npm"
# directory: "/"
# schedule:
# interval: "weekly"
# groups:
# npm:
# patterns:
# - "*"
# commit-message:
# prefix: "(chore)"
# versioning-strategy: lockfile-only

# Maintain dependencies for Composer
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "weekly"
groups:
composer:
patterns:
- "*"
commit-message:
prefix: "(chore)"
versioning-strategy: lockfile-only
15 changes: 15 additions & 0 deletions .github/workflows/badges.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Create Badges

on:
release:
types: [published]
workflow_dispatch:

permissions:
contents: write
pull-requests: write

jobs:
composer-diff:
uses: yardinternet/workflows/.github/workflows/badges.yml@main
secrets: inherit
10 changes: 10 additions & 0 deletions .github/workflows/composer-lock-diff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Composer Diff
on:
pull_request:
paths:
- 'composer.lock'

jobs:
composer-diff:
uses: yardinternet/workflows/.github/workflows/composer-lock-diff.yml@main
secrets: inherit
7 changes: 7 additions & 0 deletions .github/workflows/dependabot-automerge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: Dependabot Automerge
on: pull_request

jobs:
dependabot-automerge:
uses: yardinternet/workflows/.github/workflows/dependabot-automerge.yml@main
secrets: inherit
11 changes: 11 additions & 0 deletions .github/workflows/format-php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Code Style

on:
pull_request:
paths:
- '**.php'

jobs:
php-cs-fixer:
uses: yardinternet/workflows/.github/workflows/format-php.yml@main
secrets: inherit
10 changes: 10 additions & 0 deletions .github/workflows/markdown-linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Markdown Linting
on:
pull_request:
paths:
- '**.md'

jobs:
linting:
uses: yardinternet/workflows/.github/workflows/markdown-linting.yml@main
secrets: inherit
13 changes: 13 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: PHPStan

on:
pull_request:
paths:
- '**.php'
- 'phpstan.neon.dist'
- '.github/workflows/phpstan.yml'

jobs:
phpstan:
uses: yardinternet/workflows/.github/workflows/phpstan.yml@main
secrets: inherit
18 changes: 18 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests

on:
pull_request:
paths:
- '**.php'
- '.github/workflows/run-tests.yml'
- 'phpunit.xml.dist'
- 'composer.json'
- 'composer.lock'
workflow_dispatch:

jobs:
test:
uses: yardinternet/workflows/.github/workflows/run-pest-tests.yml@main
with:
php-versions: '["7.4","8.1","8.2","8.3","8.4"]'
secrets: inherit
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
.php-cs-fixer.cache
.phpunit.result.cache
build
vendor
6 changes: 6 additions & 0 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
MD010: { "spaces_per_tab": 4 }
MD013: false
MD024: { "siblings_only": true }
MD025: false
MD033: false
MD036: false
21 changes: 21 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Finder;
use Yard\PhpCsFixerRules\Config;

# PHP CS Fixer can be run by using the composer script `composer format`

$finder = Finder::create()
->in(__DIR__)
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true)
->exclude('public')
->exclude('node_modules')
->exclude('build')
->append(['.php-cs-fixer.php']);

return Config::create($finder);
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Yard | Digital Agency

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 THE
SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Replaceable PSR-3 Logger

[![Code Style](https://github.com/yardinternet/logger/actions/workflows/format-php.yml/badge.svg?no-cache)](https://github.com/yardinternet/logger/actions/workflows/format-php.yml)
[![PHPStan](https://github.com/yardinternet/logger/actions/workflows/phpstan.yml/badge.svg?no-cache)](https://github.com/yardinternet/logger/actions/workflows/phpstan.yml)
[![Tests](https://github.com/yardinternet/logger/actions/workflows/run-tests.yml/badge.svg?no-cache)](https://github.com/yardinternet/logger/actions/workflows/run-tests.yml)
[![Code Coverage Badge](https://github.com/yardinternet/logger/blob/badges/coverage.svg)](https://github.com/yardinternet/logger/actions/workflows/badges.yml)
[![Lines of Code Badge](https://github.com/yardinternet/logger/blob/badges/lines-of-code.svg)](https://github.com/yardinternet/logger/actions/workflows/badges.yml)

Composer package that provides a very basic PSR-3 logger. This logger is just a wrapper around PHP function `error_log()`.
As such it does not have any requirements other than `psr/log`. It is meant to be used in PHP environments that might not
feature a DI container, like WordPress.

The provided logger is actually meant as a temporary stub. The most prominent feature of this package is that the logger
can be replaced by any another PSR-3 logger. This allows projects to push a single logger to its dependencies and achieve
consistent log handling.

## Installation

To install this package using Composer, follow these steps:

1. Add the following to the `repositories` section of your `composer.json`:

```json
{
"type": "vcs",
"url": "git@github.com:yardinternet/logger.git"
}
```

2. Install this package with Composer:

```sh
composer require yard/logger
```

## Usage

Simply use the static `Log` facade:

```php
Log::error('Whoops.');
```

Or get the PSR-3 logger instance from it:

```php
$logger = Log::getLogger();
```

Projects that wish to replace the logger instance can use the static `setLogger()` method, like this:

```php
Log::setLogger(app()->make('log'));
```

### WordPress

When using the logger in WordPress themes, the above replacement method is not recommended, due to the practise of vendor prefixing. The recommended method to replace the logger from a WordPress theme is to:

1. Use `do_action()` in WordPress themes to pass the desired logger to any plugins
2. Use `add_action()` in WordPress plugins to receive a logger instance and set it.

The `Log` class provides the `WP_ACTION_SET_LOGGER` constant, which contains the name of the WordPress action that should be used. Hooking into the action should look like:

```php
add_action(Yard\Logging\Log::WP_ACTION_SET_LOGGER, Log::setLogger(...));
```

An [Acorn](https://roots.io/acorn/) based WordPress theme for example could push its Laravel logger like this:

```php
do_action(Yard\Logging\Log::WP_ACTION_SET_LOGGER, app()->make('log'));
```
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "yard/logger",
"type": "package",
"description": "Basic replaceable PSR-3 logger",
"license": "MIT",
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"require": {
"php": ">=7.4",
"psr/log": "^1.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"pestphp/pest": "^1.0",
"phpstan/phpstan": "^2.0",
"yard/php-cs-fixer-rules": "^1.0"
},
"autoload": {
"psr-4": {
"Yard\\Logging\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Yard\\Logging\\Tests\\": "tests/"
}
},
"scripts": {
"all": [
"@test",
"@analyse",
"@format"
],
"analyse": "vendor/bin/phpstan analyse --debug --memory-limit 1G",
"format": "vendor/bin/php-cs-fixer fix",
"test": "vendor/bin/pest",
"test:coverage": "XDEBUG_MODE=coverage vendor/bin/pest --coverage"
}
}
Loading
Loading