{"id":3100,"date":"2025-09-22T17:15:03","date_gmt":"2025-09-22T17:15:03","guid":{"rendered":"https:\/\/codingfix.com\/?p=3100"},"modified":"2025-10-25T11:14:17","modified_gmt":"2025-10-25T11:14:17","slug":"log-things-to-debug-custom-log","status":"publish","type":"post","link":"https:\/\/codingfix.com\/log-things-to-debug-custom-log\/","title":{"rendered":"Log things to debug-custom.log"},"content":{"rendered":"\n<div class=\"wp-block-greenshift-blocks-row gspb_row gspb_row-id-gsbp-38d0038\" id=\"gspb_row-id-gsbp-38d0038\"><div class=\"gspb_row__content\"> \n<div class=\"wp-block-greenshift-blocks-row-column gspb_row__col--12 gspb_col-id-gsbp-eac17c2\" id=\"gspb_col-id-gsbp-eac17c2\">\n<h2 id=\"gspb_heading-id-gsbp-0a747f1\" class=\"gspb_heading gspb_heading-id-gsbp-0a747f1 \">Log things to debug-custom.log<\/h2>\n\n\n\n<div id=\"gspb_text-id-gsbp-a2e0f0a\" class=\"gspb_text gspb_text-id-gsbp-a2e0f0a \">If you have WP_DEBUG set to true, you have probably noticed that the debug.log file soon ends up getting messed up with messages that don&#8217;t entangle you at all. But you can create an alternative log file where only your messages will be logged! In the functions.php file add this:<br><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>function custom_log( $message ) {\n\t$log_file = WP_CONTENT_DIR . '\/debug-custom.log';\n\tif ( is_array( $message ) || is_object( $message ) ) {\n\t\terror_log( print_r( '&#91;' . gmdate( 'Y-m-d H:i:s' ) . '&#93; ' . $message, true ), 3, $log_file );\n\t} else {\n\t\terror_log( '&#91;' . gmdate( 'Y-m-d H:i:s' ) . '&#93; ' . $message . \"\\n\", 3, $log_file );\n\t}\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">custom_log<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #001080\">$message<\/span><span style=\"color: #000000\"> ) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #001080\">$log_file<\/span><span style=\"color: #000000\"> = WP_CONTENT_DIR <\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #A31515\">&#39;\/debug-custom.log&#39;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> ( <\/span><span style=\"color: #795E26\">is_array<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #001080\">$message<\/span><span style=\"color: #000000\"> ) || <\/span><span style=\"color: #795E26\">is_object<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #001080\">$message<\/span><span style=\"color: #000000\"> ) ) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t\t<\/span><span style=\"color: #795E26\">error_log<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #795E26\">print_r<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #A31515\">&#39;&#91;&#39;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">gmdate<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #A31515\">&#39;Y-m-d H:i:s&#39;<\/span><span style=\"color: #000000\"> ) <\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #A31515\">&#39;&#93; &#39;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">$message<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">true<\/span><span style=\"color: #000000\"> ), <\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">$log_file<\/span><span style=\"color: #000000\"> );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t} <\/span><span style=\"color: #AF00DB\">else<\/span><span style=\"color: #000000\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t\t<\/span><span style=\"color: #795E26\">error_log<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #A31515\">&#39;&#91;&#39;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">gmdate<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #A31515\">&#39;Y-m-d H:i:s&#39;<\/span><span style=\"color: #000000\"> ) <\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #A31515\">&#39;&#93; &#39;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">$message<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">$log_file<\/span><span style=\"color: #000000\"> );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div id=\"gspb_text-id-gsbp-b20d49b\" class=\"gspb_text gspb_text-id-gsbp-b20d49b \"><br><em>To log only what is really important to you! <\/em>\ud83d\udcdc<\/div>\n<\/div>\n <\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":3,"featured_media":3051,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-single-ninja-post","format":"standard","meta":{"_gspb_post_css":"#gspb_row-id-gsbp-38d0038{justify-content:space-between;margin-top:0;display:flex;flex-wrap:wrap;margin-bottom:0}#gspb_row-id-gsbp-38d0038>.gspb_row__content{display:flex;justify-content:space-between;margin:0 auto;width:100%;flex-wrap:wrap}.gspb_row{position:relative}div[id^=gspb_col-id]{box-sizing:border-box;position:relative;padding:var(--gs-row-column-padding, 15px min(3vw, 20px))}body.gspb-bodyfront #gspb_row-id-gsbp-38d0038>.gspb_row__content{max-width:var(--wp--style--global--wide-size, 1200px)}#gspb_col-id-gsbp-eac17c2.gspb_row__col--12{width:100%}@media (max-width:575.98px){#gspb_col-id-gsbp-eac17c2.gspb_row__col--12{width:100%}}.gspb_text-id-gsbp-b20d49b{margin-bottom:0!important}","footnotes":""},"categories":[134],"tags":[],"post_folder":[144],"class_list":["post-3100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wp-sniptips"],"_links":{"self":[{"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/posts\/3100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/comments?post=3100"}],"version-history":[{"count":2,"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/posts\/3100\/revisions"}],"predecessor-version":[{"id":3166,"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/posts\/3100\/revisions\/3166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/media\/3051"}],"wp:attachment":[{"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/media?parent=3100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/categories?post=3100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/tags?post=3100"},{"taxonomy":"post_folder","embeddable":true,"href":"https:\/\/codingfix.com\/wp-json\/wp\/v2\/post_folder?post=3100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}