Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Need extended information on Logging #434

Closed
astorm opened this issue Dec 3, 2015 · 16 comments
Closed

Need extended information on Logging #434

astorm opened this issue Dec 3, 2015 · 16 comments
Assignees
Labels
Contribution Day Issues and PRs for Contribution Days! Groomed and ready to work. Help Wanted Help wanted for issue/PR New Topic A major update published as an entirely new document Special achievement High-value contributions that earn higher rewards and special attention

Comments

@astorm
Copy link
Contributor

astorm commented Dec 3, 2015

Original Ticket Suggestion

Suggestion for http://devdocs.magento.com/guides/v2.0/howdoi/bk-how-do-i.html

How do module developers log a message to system.log when creating their own modules?
How do module developers log a message to a custom log file?

Existing content in DevDocs

https://devdocs.magento.com/guides/v2.2/config-guide/log/log-intro.html
https://devdocs.magento.com/guides/v2.2/config-guide/log/log-magento.html
https://devdocs.magento.com/guides/v2.2/config-guide/log/log-db.html

Need more information!

Read through the comments for ideas and deeper technical information we could use in these topics!

@astorm astorm changed the title How do I Log a Message to system.log and other log files How do I: Log a Message to system.log and other log files Dec 3, 2015
@ghost ghost self-assigned this Dec 15, 2015
@ghost
Copy link

ghost commented Dec 15, 2015

What do you think about this? If it's suitable we'll turn it into a How Do I topic.

@astorm
Copy link
Contributor Author

astorm commented Dec 16, 2015

@xcomSteveJohnson Yup, that's a good template to use for a dev docs argument, and the sort of thing I was thinking about.

@ghost
Copy link

ghost commented Jan 11, 2016

Thank you. Created internal issue MAGETWO-47820 to track

@ghost
Copy link

ghost commented Jul 14, 2016

@astorm Let us know what you think: http://bit.ly/29xO5Hd

@astorm
Copy link
Contributor Author

astorm commented Jul 14, 2016

A good first draft, but it doesn't go far enough.

You mention

Log to files and syslog, Send alerts and e-mails, Log specific servers and networked logging, Logging in development (integration with FireBug and ChromePHP, among others), Log to the database

However, I don't see any concrete example of how this works. i.e. you say you can log to files, but the three articles don't show which files get logged to. Some like like

  1. Write this code
  2. Run this code
  3. Look in file x on your system for the message

It's also not clear from the above if Magento has stock loggers for logging to email, Chrome, etc, or if you're just pointing out one could implement something like this themselves.

@mttjohnson
Copy link

Agreed. It is a nice first draft, but does lack a lot of details for understanding how to utilize a logger affectively.

There are two handlers currently defined in the Magento di.xml that will write to the /var/log/debug.log and /var/log/system.log.

<type name="Magento\Framework\Logger\Monolog">
    <arguments>
        <argument name="name" xsi:type="string">main</argument>
        <argument name="handlers"  xsi:type="array">
            <item name="system" xsi:type="object">Magento\Framework\Logger\Handler\System</item>
            <item name="debug" xsi:type="object">Magento\Framework\Logger\Handler\Debug</item>
        </argument>
    </arguments>
</type>

The way Monolog works is that it has the capability to "bubble" a message from one handler on to the next one in the list if the handler allows the record to be bubbled. When the System handler's constructor is called when instantiated it will build a handler that isHandling() any records that have a level >= Logger::INFO. Any records that the Logger addRecord() receives will get evaluated to see if the handlers in the list will handle them or not.

So in instances of using $this->logger->critical($e) like your example, the Logger addRecord() method would check to see which handlers in the list are able to handle the record, and because the System handler would accept handling a critical record (Logger::CRITICAL) the System handler will process the write for the record and one would expect it to write to the /var/log/system.log file, but that's not what happens. The Magento\Framework\Logger does some checking of the message (record) that it gets in it's addRecord() method and if it happens to be an instance of \Exception as in your example it will set the $context['is_exception'] variable to true and when the System handler goes to write the record, the System handler will see that $record['context']['is_exception'] is true and decide not to write itself and instead pass the record over to a different handler, the Exception handler. The Exception handler then writes to the /var/log/exception.log file.

If you are going to make a few examples, I'd suggest doing some $this->logger->debug("hello world") and $this->logger->critical("hello world") examples in addition to your exception so that you can demonstrate which ones get written to the three common log files exception.log, system.log and debug.log.

As far as extending the capabilities of Monolog for the other ways you can use logging, like you hint at in logging to a database, you may find that providing an example of that is a bit more difficult because that kind of capability would be provided by additional handlers. There are some fundamental issues with adding any additional handlers to the Magento\Framework\Logger\Monolog see Magento 2 Issue #2529. Aside from any architectural concerns, there is a bug in the version of Monolog that Magento 2 is currently using that prevents any bubbling of messages to any successive handlers, and only the first handler in the list of handlers that accept the record is getting called.

I implemented a separate Monolog file handler and database handler, but wasn't able to find a way to utilize the Magento Logger, so I ended up writing my own Logger and injecting it instead of the \Psr\Logger\LoggerInterface and I wasn't happy with the solution I came up with. If you want to see an implementation of using a handler to write to the database, or change how things are written to a separate log file, you could see some implementations here, but I don't know how you'd want to go about demonstrating the use of that while using the Magento Logger.

@ghost
Copy link

ghost commented Jul 14, 2016

@mttjohnson Thanks for your input. We'll keep you posted.

@ghost ghost added the Waiting for Response Waiting for response from internal/external parties label Oct 23, 2016
magento-cicd2 pushed a commit that referenced this issue Nov 2, 2016
Fixed FTF guide name (ds_ftf-guide-rename)
@astorm
Copy link
Contributor Author

astorm commented Dec 20, 2016

Any update on is, or is this not going to happen?

@ghost
Copy link

ghost commented Dec 21, 2016

I'm waiting on input from engineering. I don't have an ETA for that.

@astorm
Copy link
Contributor Author

astorm commented Dec 21, 2016

Thanks for the update @xcomSteveJohnson -- we live in hope :)

@jcalcaben jcalcaben self-assigned this May 11, 2017
@jeff-matthews jeff-matthews changed the title How do I: Log a Message to system.log and other log files Community contribution: Logging Feb 28, 2018
@jeff-matthews
Copy link
Contributor

Help us improve the Custom logging topic by adding more details and examples. Refer to our Contribution guidelines for more information.

Acceptance criteria:
Your content must meet the following criteria:

  • Describe what you can do with Magento's stock logger implementation and when you should extend it or implement your own.
  • Explain how to log a message to the system.log file.
  • Explain how to log a message to a custom log file.
  • Provide working sample code for each of the scenarios listed in the topic, including what code to run and where to look in the file system for the log message:
    • Log to files and syslog
    • Send alerts and emails
    • Log specific servers and networked logging
    • Logging in development
  • Differentiate between versions of Magento, if applicable.

@jeff-matthews jeff-matthews added Help Wanted Help wanted for issue/PR and removed Waiting for Response Waiting for response from internal/external parties labels Apr 3, 2018
@shrielenee
Copy link
Contributor

Hi @RakeshJesadiya - Do you want to pick up work on this? We feel like you may have the answers we need!

@webkul-ratnesh
Copy link

webkul-ratnesh commented Oct 4, 2018

Generally i use this old Magento 1 technique to print all logs in our custom modules, by adding this much code in helper.
https://gist.github.com/webkul-ratnesh/5a7df043fef8723a88ea4da215ea29ad
@astorm
I hope this will help.
#SQUASHTOBERFEST

@shrielenee shrielenee added the Special achievement High-value contributions that earn higher rewards and special attention label Mar 22, 2019
@lorikrell lorikrell removed New Topic A major update published as an entirely new document New doc labels Mar 25, 2019
@dshevtsov dshevtsov added the New Topic A major update published as an entirely new document label Mar 25, 2019
@lorikrell lorikrell changed the title Community contribution: Logging Need extended information on Logging May 31, 2019
@lorikrell lorikrell added the Contribution Day Issues and PRs for Contribution Days! Groomed and ready to work. label May 31, 2019
@konarshankar07
Copy link
Contributor

Hello
Do we need to add the new acceptance criteria in new menu or we need to separate the acceptance criteria into different menu?
Thanks

@andrewbess
Copy link
Contributor

Hello guys. I think I can describe it. Could you please assign this issue to me?

@atwixfirster
Copy link
Contributor

atwixfirster commented Dec 27, 2019

How do module developers log a message to a custom log file?

Already described in https://devdocs.magento.com/guides/v2.2/config-guide/log/custom-logger-handler.html

@astorm, could you please let us know if the mentioned topic answers on your questions?

Many thanks and Merry Christmas!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Contribution Day Issues and PRs for Contribution Days! Groomed and ready to work. Help Wanted Help wanted for issue/PR New Topic A major update published as an entirely new document Special achievement High-value contributions that earn higher rewards and special attention
Projects
None yet
Development

No branches or pull requests