php-monolog.json 7.3 KB

123456
  1. {
  2. "html": "<div class=\"section\" id=\"installation\">\n<h2>Installation</h2>\n<p>There are various ways to install the PHP integration for Sentry. The\nrecommended way is to use <a class=\"reference external\" href=\"http://getcomposer.org/\">Composer</a>:</p>\n<div class=\"highlight-python\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span>$ composer require &quot;sentry/sentry&quot;\n</pre></div>\n</div>\n<p>Alternatively you can manually install it:</p>\n<ol class=\"arabic\">\n<li><p class=\"first\">Download and extract the latest <a class=\"reference external\" href=\"https://github.com/getsentry/sentry-php/archive/master.zip\">sentry-php</a> archive\nto your PHP project.</p>\n</li>\n<li><p class=\"first\">Require the autoloader in your application:</p>\n<div class=\"highlight-php\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #2eb0f7\">require_once</span> <span style=\"color: #e8535a\">&#39;/path/to/Raven/library/Raven/Autoloader.php&#39;</span><span style=\"color: #111111\">;</span>\n<span style=\"color: #111111\">Raven_Autoloader</span><span style=\"color: #4b4f5c\">::</span><span style=\"color: #a47ac6\">register</span><span style=\"color: #111111\">();</span>\n</pre></div>\n</div>\n</li>\n</ol>\n</div>\n\n\n<div class=\"section\" id=\"monolog\">\n<h2>Monolog</h2>\n<div class=\"section\" id=\"capturing-errors\">\n<h3>Capturing Errors</h3>\n<p>Monolog supports Sentry out of the box, so you&#8217;ll just need to configure a handler:</p>\n<div class=\"highlight-php\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #111111\">$client</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #2eb0f7\">new</span> <span style=\"color: #111111\">Raven_Client(</span><span style=\"color: #e8535a\">&#39;___DSN___&#39;</span><span style=\"color: #111111\">);</span>\n\n<span style=\"color: #111111\">$handler</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #2eb0f7\">new</span> <span style=\"color: #111111\">Monolog\\Handler\\RavenHandler($client);</span>\n<span style=\"color: #111111\">$handler</span><span style=\"color: #4b4f5c\">-&gt;</span><span style=\"color: #a47ac6\">setFormatter</span><span style=\"color: #111111\">(</span><span style=\"color: #2eb0f7\">new</span> <span style=\"color: #111111\">Monolog\\Formatter\\LineFormatter(</span><span style=\"color: #e8535a\">&quot;%message% %context% %extra%\\n&quot;</span><span style=\"color: #111111\">));</span>\n\n<span style=\"color: #111111\">$monolog</span><span style=\"color: #4b4f5c\">-&gt;</span><span style=\"color: #a47ac6\">pushHandler</span><span style=\"color: #111111\">($handler);</span>\n</pre></div>\n</div>\n</div>\n<div class=\"section\" id=\"adding-context\">\n<h3>Adding Context</h3>\n<p>Capturing context can be done via a monolog processor:</p>\n<div class=\"highlight-php\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #111111\">$monolog</span><span style=\"color: #4b4f5c\">-&gt;</span><span style=\"color: #a47ac6\">pushProcessor</span><span style=\"color: #111111\">(</span><span style=\"color: #2eb0f7\">function</span> <span style=\"color: #111111\">($record)</span> <span style=\"color: #111111\">{</span>\n <span style=\"color: #34c08b; font-style: italic\">// record the current user</span>\n <span style=\"color: #111111\">$user</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #111111\">Acme</span><span style=\"color: #4b4f5c\">::</span><span style=\"color: #a47ac6\">getCurrentUser</span><span style=\"color: #111111\">();</span>\n <span style=\"color: #111111\">$record[</span><span style=\"color: #e8535a\">&#39;context&#39;</span><span style=\"color: #111111\">][</span><span style=\"color: #e8535a\">&#39;user&#39;</span><span style=\"color: #111111\">]</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #2eb0f7\">array</span><span style=\"color: #111111\">(</span>\n <span style=\"color: #e8535a\">&#39;name&#39;</span> <span style=\"color: #4b4f5c\">=&gt;</span> <span style=\"color: #111111\">$user</span><span style=\"color: #4b4f5c\">-&gt;</span><span style=\"color: #a47ac6\">getName</span><span style=\"color: #111111\">(),</span>\n <span style=\"color: #e8535a\">&#39;username&#39;</span> <span style=\"color: #4b4f5c\">=&gt;</span> <span style=\"color: #111111\">$user</span><span style=\"color: #4b4f5c\">-&gt;</span><span style=\"color: #a47ac6\">getUsername</span><span style=\"color: #111111\">(),</span>\n <span style=\"color: #e8535a\">&#39;email&#39;</span> <span style=\"color: #4b4f5c\">=&gt;</span> <span style=\"color: #111111\">$user</span><span style=\"color: #4b4f5c\">-&gt;</span><span style=\"color: #a47ac6\">getEmail</span><span style=\"color: #111111\">(),</span>\n <span style=\"color: #111111\">);</span>\n\n <span style=\"color: #34c08b; font-style: italic\">// Add various tags</span>\n <span style=\"color: #111111\">$record[</span><span style=\"color: #e8535a\">&#39;context&#39;</span><span style=\"color: #111111\">][</span><span style=\"color: #e8535a\">&#39;tags&#39;</span><span style=\"color: #111111\">]</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #2eb0f7\">array</span><span style=\"color: #111111\">(</span><span style=\"color: #e8535a\">&#39;key&#39;</span> <span style=\"color: #4b4f5c\">=&gt;</span> <span style=\"color: #e8535a\">&#39;value&#39;</span><span style=\"color: #111111\">);</span>\n\n <span style=\"color: #34c08b; font-style: italic\">// Add various generic context</span>\n <span style=\"color: #111111\">$record[</span><span style=\"color: #e8535a\">&#39;extra&#39;</span><span style=\"color: #111111\">][</span><span style=\"color: #e8535a\">&#39;key&#39;</span><span style=\"color: #111111\">]</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #e8535a\">&#39;value&#39;</span><span style=\"color: #111111\">;</span>\n\n <span style=\"color: #2eb0f7\">return</span> <span style=\"color: #111111\">$record;</span>\n<span style=\"color: #111111\">});</span>\n</pre></div>\n</div>\n</div>\n<div class=\"section\" id=\"breadcrumbs\">\n<h3>Breadcrumbs</h3>\n<p>Sentry provides a breadcrumb handler to automatically send logs along as crumbs:</p>\n<div class=\"highlight-php\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #111111\">$client</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #2eb0f7\">new</span> <span style=\"color: #111111\">Raven_Client(</span><span style=\"color: #e8535a\">&#39;___DSN___&#39;</span><span style=\"color: #111111\">);</span>\n\n<span style=\"color: #111111\">$handler</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #2eb0f7\">new</span> <span style=\"color: #111111\">\\Raven_Breadcrumbs_MonologHandler($client);</span>\n<span style=\"color: #111111\">$monolog</span><span style=\"color: #4b4f5c\">-&gt;</span><span style=\"color: #a47ac6\">pushHandler</span><span style=\"color: #111111\">($handler);</span>\n</pre></div>\n</div>\n</div>\n</div>\n",
  3. "link": "https://docs.getsentry.com/clients/php/integrations/monolog/",
  4. "id": "php-monolog",
  5. "name": "Monolog"
  6. }