Write log in linux

How can I log to a specific file in linux using logger command?

But I don’t want log message to be written in /var/log/messages because I don’t have root privileges. Instead I want it to be written to a file in my home directory: /home/myuser/mylog How should I modify logger command above?

7 Answers 7

I don’t think you really need to (or want to) involve logger / syslog for this. Simply replace the last line of the script with:

echo "Exit code of my program is $exitvalue" >> /some/file/that/you/can/write/to 

Proper timestamping would be lost this way, he’d have to do it manually then. Also, to support priority levels and/or tags (which are very common features of even the most primitive loggers, e.g. differentiating success, error, warning etc.), he’d have to implement, that, too from scratch. (Been there, done that, and after a few halfhearted attempt I just ended up with logger, too.)

@Sz. Those observations are all correct. But, aside from customizing the configuration of syslog (or syslog-ng or rsyslog , whatever version his distribution is using), which he can’t do without root privilege, rolling your own, possibly with the aid of certain Python/Java/whatever libraries are available, is pretty much the only way to get the logs into a file that you control.

The short «official» answer is that, unfortunately, you can’t.

However, in most cases (e.g. on many Linux distros) you may just be lucky enough to have a logger implementation that both supports the —no-act option, and also implements some message formatting on its own (see below), in which case you can use a (moderately ugly) command like this to put a) a properly formatted message, b) to a file, c) not polluting the system logs:

logger --no-act -s "Oh dear. " 2>&1 | sed 's/^//' >> /tmp/my.log 

(Shout out to @BasileStarynkevitch, and @Kieveli, who both mentioned parts of it before, just not the whole story.)

1) To match the usual log file format, I had to «sed off» the field ( PRIVAL = FACILITY * 8 + PRIORITY ) that got prepended to the output on my Debian boxes. Your mileage may vary.

2) POSIX itself does not define how exactly the logger command should treat (any of) its options. E.g. the GNU logger does not support —no-act at all. Also, when posting the original version of this answer 2 years ago, -s on my system did not do any formatting to the printed output, it just echoed the raw message alone, rendering it completely useless. (I didn’t use Systemd at that time, which might explain the difference, seeing various conditional Systemd-related calls in the logger source code, but this is just vague guesswork.)

Читайте также:  Установка 1с платформа linux

3) The most likely reason why the logger command has been so «historically unfriendly» for this trivial use case is that it’s just a fairly simple frontend to the system logger. This means that anything you feed to it basically goes right through to syslogd (or systemd-journald etc.), which, in turn, does all sorts of other further processing, and dispatching (to various outlets: files, sockets etc., both local and remote), as well as bridging permission levels (so in your example you could still log to syslog or user.log , for instance, even though you may have no permission to write to those files directly).

For logger to be able to properly log to a file directly, it would either have to (re)implement some of the duties of the system logger, and the syslog() std. library function, or it would be not much more capable than a trivial echo one-liner (less complex, perhaps, even than the above logger invocation! 😉 ). Either way, that seems like a bad idea.

A better solution could be if the POSIX interface (logger, syslog()) had a way to specify an ad-hoc outlet/facility parameter (like a filename) along with the message, so one could log to custom files without reconfiguring the system (which normal users have no permission to do anyway).

However, for the lack of anything better, the «canonical» Linux logger implementation actually does seem to duplicate some of the syslog functionality, so most of us can just enjoy that luxury for free. 😉

Источник

How to Use Linux Logger Command

In UNIX and Linux-type operating systems, the log is a file that records each action of the operating system. Whenever a user login to the system, it saves the record in the log file. It also allows the user to add any content to the file.

For this, the term “logger” is the command-line tool that provides a shell command interface and gives the user an easy approach to add logs in the /var/log/syslog files. You can add entries into the log files using the “logger” command.

The syntax of this command-line utility is:

How to Use logger Command with Options:

The “logger” command is a pre-built tool in Linux systems. Using this command, users can perform various functions with different options:

The syslog file plays an important role in Linux distributions as it stores all the log data in the /var/log directory.

To view the syslog file in the terminal, execute the following tail command:

Specify the syslog Lines:

The “tail” is used to capture the record from syslog files and print it in the terminal. By default, when a tail command is executed, it prints the last 10 log lines of a file. But we can also specify the number of log lines to print:

Читайте также:  Восстановление файловой системы linux fsck

Add log into syslog file:

Add any comment in the syslog file through the “logger” command without passing any option.

Run the “tail” command to print it on the terminal:

Log “who” Command:

The “logger” command can also be used to add the standard output of any command. Type the “who” with logger command to add it in the syslog file:

Display it with the tail command:

Log Specified File:

The “logger” command allows the user to add the content of a specified file into the syslog file using the “-f” option.

Let’s create a file named “test_file1.txt” and add some text to it:

Now, to print the file log in the terminal, execute the given command:

NOTE: In the tail command, tail -2 means that it will print the last two output lines. But if you want to print the detailed output with all the logs, you don’t need to specify the number of lines.

Specify Log Size:

Some loglines can be long strings and limit them to use “–size” option. Run the mentioned “–size” option in the following way:

(In the above command, we added random characters in the log and displayed the only first 12 characters using the size option. Tail -1 will print only the last line of the display result).

Ignore Empty Lines:

Use the “-e” option if the file contains empty lines in it. It will remove the blank lines from the file and print the output in the standard way.

For example, add some blank lines in the text file we created:

Run the “-e” option with the file name “test_file1.txt” to remove empty lines:

Display Help:

Type the “–help” option to display the help message about the “logger” command and its options:

Conclusion:

The “syslog” file in every system keeps a record of each action performed by the operating system. There is a “logger” command in the Linux systems that provides an interface to the user to add logs in the “/var/log/syslog” file using the terminal.

In this writing, we have discussed the Linux “logger” command and learned the functionality of its different options through multiple examples.

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.

Читайте также:  Mysql programming in linux

Источник

make a log file

Is there any way to make a log file for maintaining some data in /var/log/ with the help of some library function or system call in c language in linux. And I also want to know the standards that we should follow to write and process log. Thanks

Please note that programming questions are generally off-topic here; you should ask them on Stack Overflow. An explanation of the API is ok here, but explanations of how to use it in C or other programming language normally belongs on Stack Overflow.

4 Answers 4

The standard way to log from a C program is syslog .

Start by including the header file:

Then early in your program, you should configure syslog by calling openlog :

openlog("programname", 0, LOG_USER); 

The first argument is the identification or the tag, which is automatically added at the start of each message. Put your program’s name here.

The second argument is the options you want to use, or 0 for the normal behavior. The full list of options is in man 3 syslog . One you might find useful is LOG_PID , which makes syslog also record the process id in the log message.

Then, each time you want to write a log message, you call syslog :

The first argument is the priority. The priority ranges from DEBUG (least important) to EMERG (only for emergencies) with DEBUG , INFO , and ERR being the most commonly used. See man 3 syslog for your options.

The second and third arguments are a format and a message, just like printf.

Which log file this appears in depends on your syslog settings.

With a default setup, it probably goes into /var/log/messages .

You can set up a custom log file by using one of the facilities in the range LOG_LOCAL0 to LOG_LOCAL7 .

openlog("programname", 0, LOG_USER); 
openlog("programname", 0, LOG_LOCAL0); 
openlog("programname", 0, LOG_LOCAL1); 

and adding a corresponding entry to /etc/syslog.conf , e.g.

local1.info /var/log/programname.log 

and restarting the syslog server, e.g.

The .info part of local1.info above means that all messages that are INFO or more important will be logged, including INFO , NOTICE , ERR (error), CRIT (critical), etc., but not DEBUG .

Or, if you have rsyslog , you could try a property-based filter, e.g.

:syslogtag, isequal, "programname:" /var/log/programname.log 

The syslogtag should contain a «:».

Or, if you are planning on distributing your software to other people, it’s probably not a good idea to rely on using LOG_LOCAL or an rsyslog filter.

In that case, you should use LOG_USER (if it’s a normal program) or LOG_DAEMON (if it’s a server), write your startup messages and error messages using syslog , but write all of your log messages to a file outside of syslog . For example, Apache HTTPd logs to /var/log/apache2/* or /var/log/httpd/* , I assume using regular open / fopen and write / printf calls.

Источник

Оцените статью
Adblock
detector