A log file usually contains a set of system events or records. The system administrator can analyze and investigate the log files to know what's really going on in a Linux system. The log files will greatly be helpful when troubleshooting systems. All log files are generated automatically and saved under a common directory – /var/log/ in your local disk in Linux. You can also change the location of these files and save them directly in the RAM. This is where Log2Ram utility comes in help. This guide explains how to save or write log files in RAM using Log2ram in Linux operating systems.
Table of Contents
Why do you want to save log files in memory?
The Log2ram is specially created for Raspberry Pi systems. Since the Raspberry Pi devices uses a micro SD card for storage (OS, libraries and user programs), you don't want to hammer the SD cards by writing the log files all the time. In that case, you can simply move the log file location to Tmpfs (RAM) and synchronize them back to the physical disk periodically. Even though, it is developed for Raspberry Pi devices, you can use Log2ram program in all Debian-based systems.
Install Log2Ram in Ubuntu Linux
Add Log2ram repository on your Raspberry Pi or other DEB-based systems like Ubuntu using command:
$ echo "deb http://packages.azlux.fr/debian/ buster main" | sudo tee /etc/apt/sources.list.d/azlux.list
Add the GPG key:
$ wget -qO - https://azlux.fr/repo.gpg.key | sudo apt-key add -
Update sources list:
$ sudo apt update
Finally, install Log2ram using command:
$ sudo apt install log2ram
You can also install Log2ram manually using source.
Download the latest Log2ram version using command:
$ curl -Lo log2ram.tar.gz https://github.com/azlux/log2ram/archive/master.tar.gz
Extract the downloaded file:
$ tar xf log2ram.tar.gz
This command will extract the contents of Log2ram archive file in a folder called "log2ram-master". Cd into this folder:
$ cd log2ram-master
Make the installation script executable:
$ chmod +x install.sh
Finally, install Log2ram using command:
$ sudo ./install.sh
Remove the log2ram-master folder:
$ cd ..
$ rm -r log2ram-master
Now, reboot your system to activate Log2ram.
$ sudo reboot
Save Or Write Log Files In RAM Using Log2ram In Linux
The Log2ram script creates a /var/log mount point in RAM. So all log files written in the /var/log folder will not actually be written in the the physical disk. Instead, all logs are written directly to the RAM i.e. tmpfs. The log file for log2ram itself will be written in /var/log/log2ram.log file.
A cron job (/etc/cron.daily/log2ram) will run every day to synchronize the contents of the tmpfs to the SD card or HDD and/or SSD. The script will also synchronize the contents of tmpfs when you shutdown the system. This way the Log2ram script helps you to prevent excessive writing in SD card.
You can verify if the files are really written in tmpfs, use any disk usage checking programs like df like below:
$ sudo df -h
You will see an output like below:
[...] log2ram 40M 532K 40M 2% /var/log [...]
You can also use the "mount" command to verify the /var/log mount point is present in the tmpfs (RAM).
$ sudo mount
Sample output:
[...] log2ram on /var/log type tmpfs (rw,nosuid,nodev,noexec,relatime,size=40960k,mode=755) [...]
Configure Log2ram
Log2ram works out of the box with default values. You can, however, customize its parameters however you please. The default configuration file of Log2ram available at /etc/log2ram.conf. If you open it on any text editing programs, you will a couple parameters as listed below.
- SIZE - This parameter defines how much space you want to allocate in the RAM to store log files. The default value is 40M.
- USE_RSYNC - If you want to synchronize the contents of RAM using rsync program, just set this parameter value as true. By default, Log2ram uses "cp" command to copy contents of tmpfs to disk.
- MAIL - This parameter is used to notify error messages. You can also disable the error system mail if there is not enough place on RAM. To disable it, simply set its value as false.
- ZL2R - This parameter enables zram compatibility in your system. It is disabled by default. To enable this option, check this link.
Update Log2ram
To update Log2ram to new available version, simply run "apt update" command.
If you've installed it manually, stop Log2ram service:
$ sudo systemctl stop log2ram
And then start the installation again as shown above.
Uninstall Log2ram
To remove Log2ram program, run:
$ sudo apt purge --remove log2ram
If it was installed manually, run:
$ chmod +x /usr/local/bin/uninstall-log2ram.sh
$ sudo /usr/local/bin/uninstall-log2ram.sh
Related read:
- How To Mount A Temporary Partition In RAM In Linux
- How To Sync Browser Profile Into Tmpfs (RAM) In Linux
- How To Change Default Sudo Log File In Linux
- How To Manage Log Files Using Logrotate In Linux
Hope this helps.
Resource:
2 comments
Hi, question, how to do this in CentOS. Since I use CentOO instead of any form of debian in Raspberry.
Need to compile source, any binary yum installed into any repo out there?
Have a look at the official github page. They have included the instructions to install it from source.