-1

I think syslog is really a good choice for service.

Besides syslog, is there any other file logging best practice ?

Is writing file to /tmp or /var/log directly appropriate?

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
linrongbin
  • 2,967
  • 6
  • 31
  • 59
  • 3
    Many services do directly write log files. Just about all of them allow you to change the log path with a command-line argument. The language you write the service in has nothing to do with this. – Ben Voigt Aug 15 '19 at 05:16

2 Answers2

1

Read logging best practice. Questions with risk to attract too opinionated answers will be closed.

Is writing file to /tmp or /var/log directly available appropriate?

A common set of permissions for /tmp:

drwxrwxrwt  root root  /tmp

So, available, yes, but appropriate, no, not for logs. /tmp is often purged on reboot.

A common set of permissions for /var/log:

drwxr-xr-x. root root   /var/log

So, it's usually only root processes that can create files there. Other processes need to have a logfile installed in advance and logrotated - or having a subdirectory with permissions allowing the process owner to create files in it.

If your system uses journald, it may be more appropriate to make use of that instead of creating your own log files as described in Linux Logging with Systemd.

Edit: Since the original question was partly about programming languages before it was edited, I'll leave this note here:

Whether to use syslog or not doesn't have anything to do with the choice of language. If you have a syslog service, it can be used by programs written in any language.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

I found that after I create a fold in /var/log, such as /var/log/myprocess/. Notice that /var/log belongs to root, and I use chown so /var/log/myprocess/ belongs to me.

So my process can create and write files directly in /var/log/myprocess/, such as /var/log/myprocess/process-log-2019-08-15.log.

linrongbin
  • 2,967
  • 6
  • 31
  • 59