Skip to main content

📋 Send Logs to Email

Automatically collect system logs from your MikroTik router and email them to yourself for remote monitoring and troubleshooting. This script captures all router events (authentication failures, interface changes, errors) and sends them on a schedule. Perfect for monitoring multiple routers or keeping offsite audit trails of network activity.

Prerequisites

  • ✅ MikroTik RouterOS device with email service enabled
  • ✅ Gmail account (or SMTP server credentials)
  • ✅ Email service already configured (see Email Backup for setup)
  • ✅ Router has internet connectivity
  • ✅ Access to RouterOS console (SSH, WebFig, or WinBox)
  • ✅ Sufficient disk space for temporary log files
info

This guide assumes you've already configured SMTP settings. If not, complete the Email Backup guide first.

Configuration Steps

Option A: Terminal Configuration

  1. Access the terminal via SSH, console, or WebFig terminal

    ssh admin@your-router-ip
  2. Create the log backup script:

    /system script add name="send-logs-email" source={
    :local reciept "your-email@example.com";
    :local clearLogs 1;

    /do {
    :log warning "===========<[[ Creating Log File ]]>===========";
    log print file="logs backup";
    :log warning "===========<[[ Sending Log File ]]>===========";
    /do {
    /tool e-mail send to=$reciept subject="Logs $[/system identity get name] - $[/system clock get date] $[/system clock get time]" file="logs backup";
    } on-error={
    :log error "===========<[[ Error in sending the email ]]>===========";
    }
    } on-error={
    :log error "===========<[[ Error in Creating Log File ]]>===========";
    }
    :delay 5s;
    :if ($clearLogs = 1) do={
    /system logging action set memory memory-lines=1;
    /system logging action set memory memory-lines=1000;
    }
    /file remove "logs backup";
    :log warning "===========<[[ Done! Log Cleared & Removed Log File ]]>===========";
    }
    tip

    Customize these settings:

    • Replace your-email@example.com with your actual email
    • Set clearLogs to 0 if you want to preserve logs on the router
    • Adjust memory-lines=1000 to control how many recent logs to keep
  3. Schedule the log email (daily at 3 AM):

    /system scheduler add name="daily-logs" on-event="send-logs-email" start-time=03:00:00 interval=1d

Option B: WebFig Configuration

  1. Navigate to System > Scripts:

    • Click the + button to add a new script
    • Name: send-logs-email
    • Paste the full script from Option A step 2
    • Click Apply
  2. Schedule the job via System > Scheduler:

    • Click the + button to add a new scheduler entry
    • Name: daily-logs
    • On Event: send-logs-email
    • Start Time: 03:00:00
    • Interval: 1d
    • Click Apply

Understanding the Script

ComponentPurpose
reciept variableEmail address to send logs to
clearLogs flag1 = clear logs after sending; 0 = keep logs
log print fileExports all logs to a temporary file
on-error blocksHandles failures gracefully with error logging
:delay 5sWaits for email to send before cleanup
memory-linesControls how many log entries to keep in memory
:local variablesScript-local variables, not persistent

Verification

  1. Test the script manually:

    /system script run send-logs-email

    Check your email inbox within 1-2 minutes.

  2. Verify log file was created and removed:

    /file print

    The "logs backup" file should not be present (it's auto-removed).

  3. Check the system log for script execution:

    log print where topics~"system"

    You should see entries like:

    "Creating Log File"
    "Sending Log File"
    "Done! Log Cleared & Removed Log File"
  4. Verify scheduler is active:

    /system scheduler print

    Confirm daily-logs is listed and enabled.

Troubleshooting

IssueCauseSolution
Email contains no logsLogs were clearedSet clearLogs = 0 to preserve logs for testing
"Error in sending the email" messageEmail service not configuredComplete Email Backup setup; test with /tool e-mail send
Large email filesToo many logs being keptReduce memory-lines value (e.g., 500 instead of 1000)
Script runs but no email receivedScript timing issueIncrease :delay to 10s; check email spam folder
"Log file already exists" errorPrevious script failed to cleanupManually remove: /file remove "logs backup"
Scheduler not runningScheduler disabled or system time wrongVerify system time: /system clock print; enable scheduler in System menu

Log Contents

Each emailed log file typically contains:

00 00:00:00 system,info router rebooted
00 00:01:23 system,info system identity changed
00 00:05:45 interface,info ether1 link up
01 08:30:12 system,warning no available certificate
02 14:22:33 user,info admin logged in from SSH
warning

Log Retention: The script clears logs by resetting memory-lines to 1, then back to 1000. This prevents unbounded log growth but means only the last 1000 entries are retained between emails.

Advanced Options

Send logs less frequently (weekly):

/system scheduler set daily-logs interval=7d

Send logs to multiple recipients:

Modify the script to loop through multiple emails:

:local emails ("email1@example.com","email2@example.com")
:foreach email in=$emails do={
/tool e-mail send to=$email subject="..." file="logs backup"
}

Include logs before clearing (for audit trails):

Set clearLogs = 0 to keep logs on the router even after emailing.

Completion

Your router now sends logs automatically!

Next steps:

  • Test the scheduler by checking your email tomorrow at the scheduled time
  • Store received log files in a centralized location for audit trails
  • Combine with Email Backup to have complete monitoring
  • Consider setting up different log email frequencies for different topics (interfaces, user activity, etc.)