Mage::Log - Write to a Custom Database table





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I have two question regarding Mage::Log.



We are working on an application using Magento. We would like to write programming errors to custom database table. Is it possible to do the same using Mage::Log.



Is it recommended to write the error log to a database table. Or should we continue using the exception.log and system.log. We have added some try catch block in our code, so if there are any exceptions occurred, then would like write it to a DB table or to log files. Which is the best practice to follow.










share|improve this question














bumped to the homepage by Community 13 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






















    0















    I have two question regarding Mage::Log.



    We are working on an application using Magento. We would like to write programming errors to custom database table. Is it possible to do the same using Mage::Log.



    Is it recommended to write the error log to a database table. Or should we continue using the exception.log and system.log. We have added some try catch block in our code, so if there are any exceptions occurred, then would like write it to a DB table or to log files. Which is the best practice to follow.










    share|improve this question














    bumped to the homepage by Community 13 hours ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      0












      0








      0








      I have two question regarding Mage::Log.



      We are working on an application using Magento. We would like to write programming errors to custom database table. Is it possible to do the same using Mage::Log.



      Is it recommended to write the error log to a database table. Or should we continue using the exception.log and system.log. We have added some try catch block in our code, so if there are any exceptions occurred, then would like write it to a DB table or to log files. Which is the best practice to follow.










      share|improve this question














      I have two question regarding Mage::Log.



      We are working on an application using Magento. We would like to write programming errors to custom database table. Is it possible to do the same using Mage::Log.



      Is it recommended to write the error log to a database table. Or should we continue using the exception.log and system.log. We have added some try catch block in our code, so if there are any exceptions occurred, then would like write it to a DB table or to log files. Which is the best practice to follow.







      magento-1.9 logging






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 2 '16 at 9:07









      nikhilnikhil

      1331315




      1331315





      bumped to the homepage by Community 13 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 13 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Mage::log() is defined in app/Mage.php



          If you want to write to a db when you do a Mage::log('somestuff') you've to modify the file Mage.php in something like this:



          public static function log($message, $level = null, $file = '', $forceLog = false)
          {
          if (!self::getConfig()) {
          return;
          }

          try {
          $logActive = self::getStoreConfig('dev/log/active');
          if (empty($file)) {
          $file = self::getStoreConfig('dev/log/file');
          }
          }
          catch (Exception $e) {
          $logActive = true;
          }

          if (!self::$_isDeveloperMode && !$logActive && !$forceLog) {
          return;
          }

          static $loggers = array();

          $level = is_null($level) ? Zend_Log::DEBUG : $level;
          $file = empty($file) ? 'system.log' : $file;

          try {
          if (!isset($loggers[$file])) {
          $logDir = self::getBaseDir('var') . DS . 'log';
          $logFile = $logDir . DS . $file;

          if (!is_dir($logDir)) {
          mkdir($logDir);
          chmod($logDir, 0750);
          }

          if (!file_exists($logFile)) {
          file_put_contents($logFile, '');
          chmod($logFile, 0640);
          }

          $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
          $formatter = new Zend_Log_Formatter_Simple($format);
          $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');
          if (!self::$_app || !$writerModel) {
          $writer = new Zend_Log_Writer_Stream($logFile);
          }
          else {
          $writer = new $writerModel($logFile);
          }
          $writer->setFormatter($formatter);
          $loggers[$file] = new Zend_Log($writer);
          }

          if (is_array($message) || is_object($message)) {
          $message = print_r($message, true);
          }

          $loggers[$file]->log($message, $level);
          // START
          $write = Mage::getSingleton("core/resource")->getConnection("core_write");
          $query = "INSERT INTO your_log_table (message, level, logfile) values (:message, :level, :logfile)";
          $binds = array( 'message' => $message, 'level' => $level, 'logfile' => $logFile);
          $write->query($query, $binds);
          // END

          }
          catch (Exception $e) {
          }
          }





          share|improve this answer
























          • Is ok to edit the Mage.php file? Will it overwritten while upgrading Magento?

            – nikhil
            Feb 2 '16 at 11:46











          • is never ok to edit a core file.

            – nino.aratari
            Feb 2 '16 at 17:19



















          0














          IMHO the line here from the Mage::log() method



          $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');


          shows how to register a own log writer, through a node in the local.xml.



          <config>        
          <global>
          <log>
          <core>
          <writer_model>Zend_Log_Writer_Db</writer_model>
          </core>
          </log>
          </global>
          <config>


          You can extend the Zend_Log_Writer_Db and create your DB connection, table and columnmapping in the construtor, beacause Mage::log() just create a new instance here.



          if (!self::$_app || !$writerModel) {
          $writer = new Zend_Log_Writer_Stream($logFile);
          } else {
          $writer = new $writerModel($logFile);
          }


          So INHO no Core changes need or for DB logging or recommended ;)



          But please correct me if I am wrong!






          share|improve this answer
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f100013%2fmagelog-write-to-a-custom-database-table%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Mage::log() is defined in app/Mage.php



            If you want to write to a db when you do a Mage::log('somestuff') you've to modify the file Mage.php in something like this:



            public static function log($message, $level = null, $file = '', $forceLog = false)
            {
            if (!self::getConfig()) {
            return;
            }

            try {
            $logActive = self::getStoreConfig('dev/log/active');
            if (empty($file)) {
            $file = self::getStoreConfig('dev/log/file');
            }
            }
            catch (Exception $e) {
            $logActive = true;
            }

            if (!self::$_isDeveloperMode && !$logActive && !$forceLog) {
            return;
            }

            static $loggers = array();

            $level = is_null($level) ? Zend_Log::DEBUG : $level;
            $file = empty($file) ? 'system.log' : $file;

            try {
            if (!isset($loggers[$file])) {
            $logDir = self::getBaseDir('var') . DS . 'log';
            $logFile = $logDir . DS . $file;

            if (!is_dir($logDir)) {
            mkdir($logDir);
            chmod($logDir, 0750);
            }

            if (!file_exists($logFile)) {
            file_put_contents($logFile, '');
            chmod($logFile, 0640);
            }

            $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
            $formatter = new Zend_Log_Formatter_Simple($format);
            $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');
            if (!self::$_app || !$writerModel) {
            $writer = new Zend_Log_Writer_Stream($logFile);
            }
            else {
            $writer = new $writerModel($logFile);
            }
            $writer->setFormatter($formatter);
            $loggers[$file] = new Zend_Log($writer);
            }

            if (is_array($message) || is_object($message)) {
            $message = print_r($message, true);
            }

            $loggers[$file]->log($message, $level);
            // START
            $write = Mage::getSingleton("core/resource")->getConnection("core_write");
            $query = "INSERT INTO your_log_table (message, level, logfile) values (:message, :level, :logfile)";
            $binds = array( 'message' => $message, 'level' => $level, 'logfile' => $logFile);
            $write->query($query, $binds);
            // END

            }
            catch (Exception $e) {
            }
            }





            share|improve this answer
























            • Is ok to edit the Mage.php file? Will it overwritten while upgrading Magento?

              – nikhil
              Feb 2 '16 at 11:46











            • is never ok to edit a core file.

              – nino.aratari
              Feb 2 '16 at 17:19
















            0














            Mage::log() is defined in app/Mage.php



            If you want to write to a db when you do a Mage::log('somestuff') you've to modify the file Mage.php in something like this:



            public static function log($message, $level = null, $file = '', $forceLog = false)
            {
            if (!self::getConfig()) {
            return;
            }

            try {
            $logActive = self::getStoreConfig('dev/log/active');
            if (empty($file)) {
            $file = self::getStoreConfig('dev/log/file');
            }
            }
            catch (Exception $e) {
            $logActive = true;
            }

            if (!self::$_isDeveloperMode && !$logActive && !$forceLog) {
            return;
            }

            static $loggers = array();

            $level = is_null($level) ? Zend_Log::DEBUG : $level;
            $file = empty($file) ? 'system.log' : $file;

            try {
            if (!isset($loggers[$file])) {
            $logDir = self::getBaseDir('var') . DS . 'log';
            $logFile = $logDir . DS . $file;

            if (!is_dir($logDir)) {
            mkdir($logDir);
            chmod($logDir, 0750);
            }

            if (!file_exists($logFile)) {
            file_put_contents($logFile, '');
            chmod($logFile, 0640);
            }

            $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
            $formatter = new Zend_Log_Formatter_Simple($format);
            $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');
            if (!self::$_app || !$writerModel) {
            $writer = new Zend_Log_Writer_Stream($logFile);
            }
            else {
            $writer = new $writerModel($logFile);
            }
            $writer->setFormatter($formatter);
            $loggers[$file] = new Zend_Log($writer);
            }

            if (is_array($message) || is_object($message)) {
            $message = print_r($message, true);
            }

            $loggers[$file]->log($message, $level);
            // START
            $write = Mage::getSingleton("core/resource")->getConnection("core_write");
            $query = "INSERT INTO your_log_table (message, level, logfile) values (:message, :level, :logfile)";
            $binds = array( 'message' => $message, 'level' => $level, 'logfile' => $logFile);
            $write->query($query, $binds);
            // END

            }
            catch (Exception $e) {
            }
            }





            share|improve this answer
























            • Is ok to edit the Mage.php file? Will it overwritten while upgrading Magento?

              – nikhil
              Feb 2 '16 at 11:46











            • is never ok to edit a core file.

              – nino.aratari
              Feb 2 '16 at 17:19














            0












            0








            0







            Mage::log() is defined in app/Mage.php



            If you want to write to a db when you do a Mage::log('somestuff') you've to modify the file Mage.php in something like this:



            public static function log($message, $level = null, $file = '', $forceLog = false)
            {
            if (!self::getConfig()) {
            return;
            }

            try {
            $logActive = self::getStoreConfig('dev/log/active');
            if (empty($file)) {
            $file = self::getStoreConfig('dev/log/file');
            }
            }
            catch (Exception $e) {
            $logActive = true;
            }

            if (!self::$_isDeveloperMode && !$logActive && !$forceLog) {
            return;
            }

            static $loggers = array();

            $level = is_null($level) ? Zend_Log::DEBUG : $level;
            $file = empty($file) ? 'system.log' : $file;

            try {
            if (!isset($loggers[$file])) {
            $logDir = self::getBaseDir('var') . DS . 'log';
            $logFile = $logDir . DS . $file;

            if (!is_dir($logDir)) {
            mkdir($logDir);
            chmod($logDir, 0750);
            }

            if (!file_exists($logFile)) {
            file_put_contents($logFile, '');
            chmod($logFile, 0640);
            }

            $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
            $formatter = new Zend_Log_Formatter_Simple($format);
            $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');
            if (!self::$_app || !$writerModel) {
            $writer = new Zend_Log_Writer_Stream($logFile);
            }
            else {
            $writer = new $writerModel($logFile);
            }
            $writer->setFormatter($formatter);
            $loggers[$file] = new Zend_Log($writer);
            }

            if (is_array($message) || is_object($message)) {
            $message = print_r($message, true);
            }

            $loggers[$file]->log($message, $level);
            // START
            $write = Mage::getSingleton("core/resource")->getConnection("core_write");
            $query = "INSERT INTO your_log_table (message, level, logfile) values (:message, :level, :logfile)";
            $binds = array( 'message' => $message, 'level' => $level, 'logfile' => $logFile);
            $write->query($query, $binds);
            // END

            }
            catch (Exception $e) {
            }
            }





            share|improve this answer













            Mage::log() is defined in app/Mage.php



            If you want to write to a db when you do a Mage::log('somestuff') you've to modify the file Mage.php in something like this:



            public static function log($message, $level = null, $file = '', $forceLog = false)
            {
            if (!self::getConfig()) {
            return;
            }

            try {
            $logActive = self::getStoreConfig('dev/log/active');
            if (empty($file)) {
            $file = self::getStoreConfig('dev/log/file');
            }
            }
            catch (Exception $e) {
            $logActive = true;
            }

            if (!self::$_isDeveloperMode && !$logActive && !$forceLog) {
            return;
            }

            static $loggers = array();

            $level = is_null($level) ? Zend_Log::DEBUG : $level;
            $file = empty($file) ? 'system.log' : $file;

            try {
            if (!isset($loggers[$file])) {
            $logDir = self::getBaseDir('var') . DS . 'log';
            $logFile = $logDir . DS . $file;

            if (!is_dir($logDir)) {
            mkdir($logDir);
            chmod($logDir, 0750);
            }

            if (!file_exists($logFile)) {
            file_put_contents($logFile, '');
            chmod($logFile, 0640);
            }

            $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
            $formatter = new Zend_Log_Formatter_Simple($format);
            $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');
            if (!self::$_app || !$writerModel) {
            $writer = new Zend_Log_Writer_Stream($logFile);
            }
            else {
            $writer = new $writerModel($logFile);
            }
            $writer->setFormatter($formatter);
            $loggers[$file] = new Zend_Log($writer);
            }

            if (is_array($message) || is_object($message)) {
            $message = print_r($message, true);
            }

            $loggers[$file]->log($message, $level);
            // START
            $write = Mage::getSingleton("core/resource")->getConnection("core_write");
            $query = "INSERT INTO your_log_table (message, level, logfile) values (:message, :level, :logfile)";
            $binds = array( 'message' => $message, 'level' => $level, 'logfile' => $logFile);
            $write->query($query, $binds);
            // END

            }
            catch (Exception $e) {
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 2 '16 at 10:52









            nino.aratarinino.aratari

            7417




            7417













            • Is ok to edit the Mage.php file? Will it overwritten while upgrading Magento?

              – nikhil
              Feb 2 '16 at 11:46











            • is never ok to edit a core file.

              – nino.aratari
              Feb 2 '16 at 17:19



















            • Is ok to edit the Mage.php file? Will it overwritten while upgrading Magento?

              – nikhil
              Feb 2 '16 at 11:46











            • is never ok to edit a core file.

              – nino.aratari
              Feb 2 '16 at 17:19

















            Is ok to edit the Mage.php file? Will it overwritten while upgrading Magento?

            – nikhil
            Feb 2 '16 at 11:46





            Is ok to edit the Mage.php file? Will it overwritten while upgrading Magento?

            – nikhil
            Feb 2 '16 at 11:46













            is never ok to edit a core file.

            – nino.aratari
            Feb 2 '16 at 17:19





            is never ok to edit a core file.

            – nino.aratari
            Feb 2 '16 at 17:19













            0














            IMHO the line here from the Mage::log() method



            $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');


            shows how to register a own log writer, through a node in the local.xml.



            <config>        
            <global>
            <log>
            <core>
            <writer_model>Zend_Log_Writer_Db</writer_model>
            </core>
            </log>
            </global>
            <config>


            You can extend the Zend_Log_Writer_Db and create your DB connection, table and columnmapping in the construtor, beacause Mage::log() just create a new instance here.



            if (!self::$_app || !$writerModel) {
            $writer = new Zend_Log_Writer_Stream($logFile);
            } else {
            $writer = new $writerModel($logFile);
            }


            So INHO no Core changes need or for DB logging or recommended ;)



            But please correct me if I am wrong!






            share|improve this answer




























              0














              IMHO the line here from the Mage::log() method



              $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');


              shows how to register a own log writer, through a node in the local.xml.



              <config>        
              <global>
              <log>
              <core>
              <writer_model>Zend_Log_Writer_Db</writer_model>
              </core>
              </log>
              </global>
              <config>


              You can extend the Zend_Log_Writer_Db and create your DB connection, table and columnmapping in the construtor, beacause Mage::log() just create a new instance here.



              if (!self::$_app || !$writerModel) {
              $writer = new Zend_Log_Writer_Stream($logFile);
              } else {
              $writer = new $writerModel($logFile);
              }


              So INHO no Core changes need or for DB logging or recommended ;)



              But please correct me if I am wrong!






              share|improve this answer


























                0












                0








                0







                IMHO the line here from the Mage::log() method



                $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');


                shows how to register a own log writer, through a node in the local.xml.



                <config>        
                <global>
                <log>
                <core>
                <writer_model>Zend_Log_Writer_Db</writer_model>
                </core>
                </log>
                </global>
                <config>


                You can extend the Zend_Log_Writer_Db and create your DB connection, table and columnmapping in the construtor, beacause Mage::log() just create a new instance here.



                if (!self::$_app || !$writerModel) {
                $writer = new Zend_Log_Writer_Stream($logFile);
                } else {
                $writer = new $writerModel($logFile);
                }


                So INHO no Core changes need or for DB logging or recommended ;)



                But please correct me if I am wrong!






                share|improve this answer













                IMHO the line here from the Mage::log() method



                $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');


                shows how to register a own log writer, through a node in the local.xml.



                <config>        
                <global>
                <log>
                <core>
                <writer_model>Zend_Log_Writer_Db</writer_model>
                </core>
                </log>
                </global>
                <config>


                You can extend the Zend_Log_Writer_Db and create your DB connection, table and columnmapping in the construtor, beacause Mage::log() just create a new instance here.



                if (!self::$_app || !$writerModel) {
                $writer = new Zend_Log_Writer_Stream($logFile);
                } else {
                $writer = new $writerModel($logFile);
                }


                So INHO no Core changes need or for DB logging or recommended ;)



                But please correct me if I am wrong!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 18 '17 at 8:59









                GeiselGeisel

                311




                311






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f100013%2fmagelog-write-to-a-custom-database-table%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What other Star Trek series did the main TNG cast show up in?

                    Berlina muro

                    Berlina aerponto