I need to have separate log files. These files should be named during code working. It is possible? Or is there another solution? Thanks in advance.
Asked
Active
Viewed 905 times
0
-
Hi, and welcome to StackOverflow. Please read [this article](https://stackoverflow.com/help/how-to-ask) on how to ask good questions on this site. Your question is rather broad, and includes no indication of what you have tried already. This site is intended to answer specific questions, or help with specific problems, perhaps try googling general questions like this, and if you have already then let us know what you have already looked at and tried. – Jake Conkerton-Darby Nov 01 '17 at 17:11
1 Answers
0
You need to Custom Monolog Handler.
1-) You can change channel name. Write lines to config.yml
monolog:
channels: [custom_channel] # channel
handlers:
custom:
type: service
channels: [custom_channel]
id: custom.handler # service name at services.yml
2-) Add services.yml
custom.handler:
class: AppBundle\Service\CustomHandler
arguments: [""]
3-) Create Custom Handler Service
class Custom Handler extends AbstractProcessingHandler {
public function __construct(){
parent::__construct();
}
protected function write (array $record) {
dump($record); die;
}
}
4-) You can use this handler.
$this->get('monolog.logger.custom_channel')->info('It is info log.');
$this->get('monolog.logger.custom_channel')->alert('ALERT!', array('http://mertblog.net'));

Mert Simsek
- 1,550
- 9
- 19
-
Thanks for answer. But your example doesn't have ability to change channel name when we start to use handler. And additional service not required for this example at my opinion. – Anton Lifanov Nov 02 '17 at 09:20
-
Monolog has $this->container->get('monolog.handler.handler_name')->getUrl(); . Is there somethig like $this->container->get('monolog.handler.handler_name')->setUrl('new_url'); ? – Anton Lifanov Nov 02 '17 at 10:24
-
what is your purpose wtih url? monolog do log for your messages. sample; `$this->get('monolog.logger.custom_channel')->info('It is info log.');`or error or alert – Mert Simsek Nov 02 '17 at 10:53
-
Url contains destination path for log file. I want to have an ability to change this path/name. Something like that $user = 'name1'; $this->container->get('monolog.handler.handler_name')->setUrl($user . '.log'); – Anton Lifanov Nov 02 '17 at 14:34
-
No, you change to your config.yml file. If you look here https://symfony.com/doc/current/logging.html under 'Handlers: Writing Logs to different Locations' title, you will see it. – Mert Simsek Nov 02 '17 at 14:52
-
I looked this documentation of course. Unfortunately symfony has no way to do it natively. But I found just now [similar case](https://stackoverflow.com/questions/29630818/monolog-logs-file-for-every-session) . I think I can try to use this idea. – Anton Lifanov Nov 02 '17 at 16:13