Skip to content

Commit

Permalink
initial import from project
Browse files Browse the repository at this point in the history
  • Loading branch information
jwong-dayspring committed Jul 21, 2023
1 parent b1fbee6 commit 9420a69
Show file tree
Hide file tree
Showing 20 changed files with 511 additions and 0 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: PHP Composer

on:
push:

jobs:
test:
runs-on: ubuntu-22.04

strategy:
fail-fast: false
matrix:
php:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
composer:
- ''
- '--prefer-lowest'

steps:
- uses: actions/checkout@v3

- name: Use PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: sqlite3, zip
coverage: xdebug
tools: composer:v2

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-dir)"
working-directory: ./

- name: cache dependencies
id: cache-dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.composer }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-${{ matrix.php }}-${{ matrix.composer }}-composer-
- name: Validate composer.json and composer.lock
run: composer validate
working-directory: ./

- name: Install dependencies
env:
COMPOSER_FLAGS: ${{ matrix.composer }}
run: composer update ${COMPOSER_FLAGS} --prefer-source
working-directory: ./

- name: Run Tests
run: composer run-script ci-test
working-directory: ./
env:
SYMFONY_DEPRECATIONS_HELPER: weak
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
/build/
/composer.lock
/.phpunit.result.cache
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/LoggingBundle.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions DayspringLoggingBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Dayspring\LoggingBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class DayspringLoggingBundle extends Bundle
{
}
28 changes: 28 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Dayspring\LoggingBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('dayspring_logging');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.

return $treeBuilder;
}
}
28 changes: 28 additions & 0 deletions DependencyInjection/DayspringLoggingExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Dayspring\LoggingBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class DayspringLoggingExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}
}
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM php:8.1

RUN apt update
RUN apt install -y git unzip

RUN curl -s http://getcomposer.org/installer | php

#
#COPY ./ /opt/bundle/
#
#WORKDIR /opt/bundle
#
#RUN php /composer.phar --dev install
#
#
#CMD ["vendor/bin/phpunit", "--coverage-text"]
17 changes: 17 additions & 0 deletions Logger/DatadogJsonFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Dayspring\LoggingBundle\Logger;

use Monolog\Formatter\JsonFormatter;

class DatadogJsonFormatter extends JsonFormatter
{
public function format(array $record)
{
// translate a DateTime object into a string so Datadog can parse it
// 2020-10-14T17:57:05.971-0700
$record['timestamp'] = $record['datetime']->format('Y-m-d\TH:i:s.vO');

return parent::format($record);
}
}
107 changes: 107 additions & 0 deletions Logger/SessionRequestProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Dayspring\LoggingBundle\Logger;

use Exception;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;

class SessionRequestProcessor
{

/** @var Session $session */
private $session;
/** @var RequestStack $requestStack */
private $requestStack;
/** @var UrlMatcherInterface|RequestMatcherInterface $matcher */
private $matcher;

private $sessionId;
private $requestId;
private $_server;
private $_get;
private $_post;

public function __construct(Session $session, RequestStack $requestStack, $matcher)
{
$this->session = $session;
$this->requestStack = $requestStack;
$this->matcher = $matcher;
}

public function __invoke(array $record)
{
if (null === $this->requestId) {
if ('cli' === php_sapi_name()) {
$this->sessionId = getmypid();
} else {
try {
$this->session->start();
$this->sessionId = $this->session->getId();
} catch (\RuntimeException $e) {
$this->sessionId = '????????';
}
}
$this->requestId = substr(uniqid(), -8);
$this->_server = array(
'http.url' => (@$_SERVER['HTTP_HOST']).'/'.(@$_SERVER['REQUEST_URI']),
'http.method' => @$_SERVER['REQUEST_METHOD'],
'http.useragent' => @$_SERVER['HTTP_USER_AGENT'],
'http.referer' => @$_SERVER['HTTP_REFERER'],
'http.x_forwarded_for' => @$_SERVER['HTTP_X_FORWARDED_FOR']
);
$this->_post = $this->clean($_POST);
$this->_get = $this->clean($_GET);
}
$record['http.request_id'] = $this->requestId;
$record['http.session_id'] = $this->sessionId;
$record['http.url'] = $this->_server['http.url'];
$record['http.method'] = $this->_server['http.method'];
$record['http.useragent'] = $this->_server['http.useragent'];
$record['http.referer'] = $this->_server['http.referer'];
$record['http.x_forwarded_for'] = $this->_server['http.x_forwarded_for'];

if ($this->requestStack->getMasterRequest()) {
$request = $this->requestStack->getMasterRequest();
$context = [
'request_uri' => $request->getUri(),
'method' => $request->getMethod(),
];
try {
if ($this->matcher instanceof RequestMatcherInterface) {
$parameters = $this->matcher->matchRequest($request);
} else {
$parameters = $this->matcher->match($request->getPathInfo());
}
$context['route'] = isset($parameters['_route']) ? $parameters['_route'] : 'n/a';
$context['route_parameters'] = $parameters;
} catch (Exception $e) {
}
if (array_key_exists('context', $record)) {
$record['context'] = array_merge($record['context'], $context);
} else {
$record['context'] = $context;
}
}

return $record;
}

protected function clean($array)
{
$toReturn = array();
foreach (array_keys($array) as $key) {
if (false !== strpos($key, 'password')) {
// Do not add
} elseif (false !== strpos($key, 'csrf_token')) {
// Do not add
} else {
$toReturn[$key] = $array[$key];
}
}

return $toReturn;
}
}
15 changes: 15 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="Dayspring\LoggingBundle\Logger\SessionRequestProcessor">
<argument type="service" id="session" />
<argument type="service" id="request_stack" />
<argument type="service" id="router" />
<tag name="monolog.processor" handler="datadog_json"/>
</service>
</services>
</container>
28 changes: 28 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Created by PhpStorm.
* User: jwong
* Date: 3/17/16
* Time: 2:18 PM
*/

namespace Dayspring\LoggingBundle\Tests\DependencyInjection;

use Dayspring\LoggingBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;
use PHPUnit\Framework\TestCase;

class ConfigurationTest extends TestCase
{

public function testConfiguration()
{
$config = array();

$processor = new Processor();
$configuration = new Configuration(array());
$config = $processor->processConfiguration($configuration, array($config));

$this->assertEquals([], $config, 'Config should be empty');
}
}
Loading

0 comments on commit 9420a69

Please sign in to comment.