Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8252615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:36:26+00:00 2026-06-08T00:36:26+00:00

I want to write my application logs in another file than the one Symfony2

  • 0

I want to write my application logs in another file than the one Symfony2 writes its own logs and system logs. I understood that I needed to create a service of my own like this :

services:
    actionslogger:
        class: Symfony\Bridge\Monolog\Logger
        arguments: [app]
        calls:
             - [pushHandler, [@actionslogger_handler]]
    actionslogger_handler:
        class: Monolog\Handler\StreamHandler       
        arguments: [%kernel.logs_dir%/actions_%kernel.environment%.log, 200]

That works fine when I use $logger = $this->get(‘actionslogger’); in my application, so that’s ok.
But I also want to use a Formatter and a Processor to manage the way my logs are written. To do that, I use this configuration :

services:
    actionslogger.formatter.session_request:
        class: Monolog\Formatter\LineFormatter
        arguments:
            - "[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%%\n"

    actionslogger.processor.session_request:
        class: My\Bundle\LogProcessor
        arguments:  [ @session ]
        tags:
            - { name: actionslogger.processor, method: processRecord }

I can use this Formatter and Processor with Symfony2 default logger with this config:

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
            formatter: actionslogger.formatter.session_request

But if I can use the Formatter with my own logger, I can’t use the Processor. Here’s my config:

services:
    actionslogger.formatter.session_request:
        class: Monolog\Formatter\LineFormatter
        arguments:
            - "[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%%\n"

    actionslogger.processor.session_request:
        class: My\Bundle\LogProcessor
        arguments:  [ @session ]
        tags:
            - { name: actionslogger.processor, channel: app, method: processRecord, handler: @actionslogger_handler }

    actionslogger:
        class: Symfony\Bridge\Monolog\Logger
        arguments: [app]
        calls:
             - [pushHandler, [@actionslogger_handler]]
    actionslogger_handler:
        class: Monolog\Handler\StreamHandler       
        arguments: [%kernel.logs_dir%/actions_%kernel.environment%.log, 200]
        calls:
             #- [pushProcessor, [???]]
             - [setFormatter, [@actionslogger.formatter.session_request]]

The tags channel and handler in the Processor’s config seems useless.

What can I do to make the Processor work with my logger?
What should I pass to the pushProcessor method in the commented line (if that could be a valid option)?

Thanks for the help.

Note: using Symfony 2.0.0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:36:27+00:00Added an answer on June 8, 2026 at 12:36 am

    Answer to myself:

    Well, I couldn’t find how to set this, so I ended up with this not-so-heavy solution :

    I create a new logger, that is quite simple, and affect it my brand new Processor in the constructor, so the files are in My/Bundle and like this:

    #LogProcessor.php
    
    use Symfony\Component\HttpFoundation\Session;
    class LogProcessor
    {
        private $session;
        private $token;
        public function __construct(Session $session)
        {
            $this->session = $session;
        }
        public function processRecord(array $record)
        {
            if (null === $this->token) {
                try {
                    $this->token = ($this->session->getId());
                } catch (\RuntimeException $e) {
                    $this->token = '????????';
                }
                $this->token .= '#' . substr(uniqid(), -8);
            }
            $record['extra']['token'] = $this->token;
            $record['extra']['context'] = "";
            foreach($record['context'] as $key=>$value) {
                $key=str_replace(array("=","#","|"),"",$key);
                $value=str_replace(array("=","#","|"),"",$value);
                $record['extra']['context'].=$key."=".$value."#";
            }
            return $record;
        }
    }
    
    #MyLogger.php
    
    use Symfony\Bridge\Monolog\Logger;
    use Symfony\Component\HttpFoundation\Session;
    use My\Bundle\LogProcessor;
    
    class MyLogger extends Logger {
        private $session;
        private $processor;
    
        public function __construct($name, Session $session)
        {
            parent::__construct($name);
            $this->session= $session;
            $this->processor= new LogProcessor($session);
            $this->pushProcessor((array($this->processor, 'processRecord')));
        }   
    }
    

    (That could even come in handy if I want to modify the addRecord method of Logger later)

    Then I create a new service like this:

    #app/config/config.yml or src/My/Bundle/Resources/config/services.yml (if imported in app/config/config.yml)
    
    services:
        mylogger:
            class: My\Bundle\MyLogger
            arguments: [app, @session]
            calls:
                 - [pushHandler, [@mylogger.handler]]
        mylogger.handler:
            class: Monolog\Handler\StreamHandler       
            arguments: [%kernel.logs_dir%/actions_%kernel.environment%.log, 200] # 200 means "INFO"
            calls:
                 - [setFormatter, [@monolog.formatter.compactformat]]
        monolog.formatter.compactformat:
            class: Monolog\Formatter\LineFormatter
            arguments:
                - "%%datetime%%|%%channel%%|%%level_name%%|%%extra.token%%|%%message%%|%%extra.context%%\n"
    

    And here I go. I can now use my new logging service, with my Formatter /and/ my Processor, and do what I want with them (here: adding session id and log context information in an easy-to-use format).

    #MyController.php
    public function myMethod() {
        ...
        $logger = $this->get('mylogger');
        $logger->info('ACCESS PAGE', array('user'=>$userName, 'IP'=>$userIP, 'section'=>$section));
        ...
    }
    

    Anyway, if someone has the answer for my 1st question (about how to link the processor on a personal logging service, directly in the config), please post here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write an application that sends text from one device to another.
I want to write an application that takes an XML-schema as input and has
I want to write an application that will automatically detect and fill the text
I want to write an application for Android devices that interacts with the surface
I want to write an application using openstreetmaps rather than mkmapview, but I'm not
I want to write an application that monitors a music player. In my app
I want to write an application for Android, a card game that can be
Hi I want to write an application that can prevent other application from using
I want to write a application list AccessPoints and when you click one, a
I want to write an application that can capture from a TCP/IP camera. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.