Logging via Logger command
Logger is a shell command interface that allows you to use Syslog system log modules and write a line of information directly from the command line to the system log file.
----------------------------------------------------------
Logger syntax
logger [options] [messages]
**options (option): * *
-D,--UDP
Use datagram (UDP) instead of using the default stream connection (TCP)
-I.,--id
Record the process ID of each time logger per row
-F,--file file_name
Record a specific file
-H,--help
Show help text and exit
-N,--server
Writes to the specified remote syslog server, using UDP instead of the built-in syslog routines
-P,--port port_num
Uses the specified UDP port. The default port number is 514
-P,--priority priority_level
Specifies the priority of the input message, which can be a number or a format specified as "Facility.level". For example: "-P local3.info" local3 the message level of this device is info. The default level is "User.notice"
-S,--stderr
Output standard error to system log.
-T,--tag tag
Specifying Tag Records
-U,--socket socket
Writes the specified socket instead of to the built-in system log routine.
-V,--version
Realistic version information and exit
**messages:** writes the content message to the log file, which can be used in conjunction with-F.
Logger with 0 exit indicates success, and greater than 0 indicates failure.
Log level
Facility
Auth: User Authorization
AUTHPRIV: Authorization and security
Cron: Scheduling Tasks
Daemon: System daemon
Kern: Kernel-related information
LPR information about the print service
Mail-related information
News information from the press server
Syslog-generated information by syslog
Information generated by the user's program, default
UUCP information generated by UUCP
Local0~7 used to define local policies
Level
Alert needs to take immediate action.
Crit Critical state
Debug debugging
Emerg System not available
Err Error status
Error state
Info Normal Message
Notice normal, but be careful.
Example:
$ logger System rebooted #往系统日志例程中写入 "system rebooted" can be viewed in/var/log/syslog
Writes to the specified log file.
Example:
$ vim/etc/rsyslog.conf #在最后一行加入local3. */var/log/my_test.log means that all messages from Local3 are recorded in/var/log/my_test.log.
$ Service Rsyslog Restart #重启rsyslog服务
$ logger-i-T "my_test"-P local3.notice "Test_info"
$ cat/var/log/my_test.log
May 5 21:27:37 gino-virtual-machine my_test[3651]: test_info
-I record process IDs on each line
-T My_test each line of records with the label "My_test"
-P local3.notice Setting the recorded device and level
"Test_info" Output information
1. To modify a file:
Edit Syslog.conf,
# vim/etc/syslog.conf
Add Local3.none to the first line so that the log of the device Local3 is not recorded in the messages file.
# Log anything (except mail) of level info or higher.
# Don ' t log private authentication messages!
*.info;mail.none;authpriv.none;cron.none;local1.none;local3.none/var/log/messages
Record all levels of device LOCAL3 information in the Userlog file,
#user Log
local3.*/var/log/userlog
Reload the syslog service configuration file
[[Email protected] slides]# service syslog restart
Reloading syslogd ... [OK]
Reloading klogd ... [OK]
2. Test:
Test the environment of the machine:
os:red Hat Enterprise Linux 5 Update 3
NETWORK:192.168.0.100/24 gateway:192.168.0.1
[[email protected] slides]# Ping 192.168.0.1 | Logger-it logger_test-p Local3.notice &
[2] 22484
Command logger-it The meaning of the parameters in Logger_test-p local3.notice:
-I record process IDs on each line
-T Logger_test each line of records with the label "Logger_test"
-P local3.notice Setting the recorded device and level
[Email protected] slides]# tail-f/var/log/userlog
OCT 6 12:48:43 Kevein logger_test[22484]: PING 192.168.0.1 (192.168.0.1) (+) bytes of data.
OCT 6 12:48:43 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=1 ttl=253 time=49.7 ms
OCT 6 12:48:44 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=2 ttl=253 time=68.4 ms
OCT 6 12:48:45 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=3 ttl=253 time=315 ms
OCT 6 12:48:46 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=4 ttl=253 time=279 ms
OCT 6 12:48:47 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=5 ttl=253 time=347 ms
OCT 6 12:48:49 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=6 ttl=253 time=701 ms
OCT 6 12:48:50 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=7 ttl=253 time=591 ms
OCT 6 12:48:51 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=8 ttl=253 time=592 ms
OCT 6 12:48:52 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=9 ttl=253 time=611 ms
OCT 6 12:48:53 kevein logger_test[22484]: bytes from 192.168.0.1:icmp_seq=10 ttl=253 time=931 ms
The output of the ping command was successfully output to the/var/log/userlog file, and the experiment was successful.
Linux Log Logger command detailed