Skip to content

Releases: BokuNoMaxi/T3-EZ-Logger

[Feature] add sendMail function to send the log file

01 Aug 11:10
Compare
Choose a tag to compare

New Feature implemented to send log files

Features:

  • predefine default sender / receiver
  • easily toggle send mail with the activate logging option
  • overwrite default sender / receiver / subject directly in the function

Function call

$ezlogger = new EZLogger("filename.log");
$ezlogger->sendMail($subject,$to,$sender);

Function:

  public function sendMail(string $subject = "", Address|string $to = "", Address|string $sender = ""): bool
  {
    if (
      $this->extensionConfiguration["activateLog"] == "1" &&
      ($to || $this->extensionConfiguration["mailReceiver"])
    ) {
      $to = $to ?: $this->extensionConfiguration["mailReceiver"];
      $mail = GeneralUtility::makeInstance(MailMessage::class);
      $mail
        ->setSubject($subject ?? 'Report from EZ Logger')
        ->setTo($to)
        ->text("Hello, \n\nA new logfile has been generated. You can find it attached to this mail. \n\nHave a nice day!")
        ->attachFromPath($this->getLogFile(), 'logfile.log');
      if ($sender || $this->extensionConfiguration["mailSender"]) {
        $sender = $sender ?: $this->extensionConfiguration["mailSender"];
        $mail
          ->from($sender)
          ->sender($sender);
      }

      return $mail->send();
    }
    return false;
  }

Automatically add new line

01 Aug 09:15
Compare
Choose a tag to compare

!!! ATTENTION !!! This works only in TYPO3 12 !!!

Add new feature to automatically add a new line in function "writeLog", this behaviour can be easily toggled depending on use case:

new function call:

 public function writeLog(array|string $msg, bool $newLine = TRUE)
...
 if (is_string($msg)) {
          fwrite($this->fileWriter, $this->timestamp . ": " . $msg);
          $this->addNewLine($newLine);
        } elseif (is_array($msg)) {
          fwrite($this->fileWriter, $this->timestamp . ": " . print_r($msg, true));
          $this->addNewLine($newLine);
...
  private function addNewLine(bool $newLine)
  {
    if($newLine)
      fwrite($this->fileWriter, "\n");
  }