I want to programmatically clear manual log-files in order to always only see the new Logging Code for the last iteration.

Is it possible (e.g. via a parameter) to only log the last iteration with the call

Mage::log("My Test Log", null, "test.log")

Or do I always have to manually delete the content of my manual log files?

Do you have a good idea, how can I achieve this with minimum effort?


I think You can use unlink command of php it will delete your old file and

Mage::log will always create a new file

here is php Manual url


  • Okay, I that's the only way.. I hoped that there might be a shorter way.. given by magento – byebb Nov 29 '16 at 10:30
  • @Matthias No there is no shorter way given by magento – Murtuza Zabuawala Nov 29 '16 at 10:31

I'm curious about the reason why you would like to only keep one line in your log.

Here are my 2 cents about logs:

  • ideally, if your store is properly developped, you should not have anything in your log. Things that get logged are usually warning and errors and need to be fixed. Once fixed they never come back to your log.
  • disabling the log to avoid having a big log file or only displaying one line in the log is a way to hide the technical debt of your code so it's not a good practice
  • if you use the log to debug things, there's probably a better way of debuging. Been here, done that, your best bet would be to use xDebug to avoid logging stuff and filling your log files.
  • Yes, I'm only using it for Debugging right now :/ Haven't heard of xDebug before – byebb Nov 29 '16 at 10:32
  • @Matthias if you're only using it for debugging, I suggest you definitely start using xDebug. Which IDE are you using ? – Raphael at Digital Pianism Nov 29 '16 at 10:33
  • I am developing in the cloud (directly on the server) -- right now its codeanywhere (i know..not the best solution) – byebb Nov 29 '16 at 10:36

You could override the Mage::log() function but I don't understand why you would want this. Log lines have dates so you can see which is the latest entry. If you want to see the latest log entry, why bother logging it? Just display the message.

Why do you want this?

  • Because I have the log file open in my editor. How can I "just display the message"? or what do you mean with that? – byebb Nov 29 '16 at 10:30
  • I mean display the message to the frontend using var_dump() or Mage::getSingleton(‘core/session’)->addNotice(‘Notice message’);. Does your editor not automatically refresh files? You can just look at the time to find the latest log entry and that will be the message of the latest iteration. – Jacques Nov 29 '16 at 10:32
  • I know of the timestamp.. but I dont' want to jump and look for the last lines especially when I deal with large Objects.. because with this I don't have to search for the Log Code Start, since it is always at the top of the file – byebb Nov 29 '16 at 10:35
  • I think overriding the log() function your best bet. Make your own function somewhere in your Helper (function log($message) {if (file_exists('var/log/module.log')) { unlink('var/log/module.log'); Mage::log($message, null, 'module.log');}}) which will delete the log file and then Magento will create it again with your log message in it. So basically the implementation of Murtuza Zabuawala's answer so if this works, accept his answer. – Jacques Nov 29 '16 at 10:43
  • I made a mistake but I can't edit my comment anymore. I erroneously put the Mage::log() call inside the if() statement as well. Here is the correct code: function log($message) {if (file_exists('var/log/module.log')) { unlink('var/log/module.log'); } Mage::log($message, null, 'module.log');} – Jacques Nov 29 '16 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.