# Logging to a custom file with sh.



## olav (Apr 20, 2012)

I'm creating a shell script that needs to log to a file.
I know there is /usr/bin/logger, but I can only get it to log to syslog. How can I use it to log to a custom file? 

Or should I "hack it" like this:

```
echo "1 april 2012 12:00 -> some text" >> /var/log/mylogfile
```


----------



## SirDice (Apr 20, 2012)

olav said:
			
		

> I know there is /usr/bin/logger, but I can only get it to log to syslog. How can I use it to log to a custom file?



You should configure syslog.conf(5). 



> Or should I "hack it" like this:
> 
> ```
> echo "1 april 2012 12:00 -> some text" >> /var/log/mylogfile
> ```


That's not a 'hack', that's the proper way to redirect output.


----------



## olav (Apr 20, 2012)

So how would I configure /etc/syslog.cong to log ZFS related things in /var/log.zfs.log?
Is that a complex task? Do I need a special facility/priority or tag to get it to work?


----------



## SirDice (Apr 20, 2012)

If it's your own shell script you can use your own facility with the logger(1) command. Then configure syslog.conf(5) to log that specific facility to a separate file.

Another option (probably easier to use for you) is to create your script and redirect the output of the script instead of the individual commands inside the script.

[cmd=]./myscript.sh >> /var/log/mylogfile[/cmd]


----------



## olav (Apr 24, 2012)

Redirecting the output is probably the best way 

Is it possible to create your own facility? How can I do this?


----------



## SirDice (Apr 24, 2012)

Hmm.. That might not have been clear. There are various 'standard' facilities and a few you can use for your own purposes. Local0 through local7 can be used, they're not assigned to anything. Cisco devices for example will log with facility local7.


----------



## aa (Apr 24, 2012)

Example, we can set ipmon() to use local4 facility in rc.conf:

```
ipmon_flags="-Ds -L4"
```

In postgresql.conf, we log to local5 with name pg5432

```
log_destination = 'syslog'
syslog_facility = 'LOCAL5'
syslog_ident = 'pg5432'
```

Distribute log according to facilty (local0 to local7), in /etc/syslog.conf:

```
local0.*		-/var/log/local/local0.log
local1.*		-/var/log/local/local1.log
local3.*		-/var/log/local/local2.log
local3.*		-/var/log/local/local3.log
local4.*		-/var/log/local/local4.log
local5.*		-/var/log/local/local5.log
local6.*;mail.none	-/var/log/local/local6.log
local7.*		-/var/log/local/local7.log
```


Or reroute to it's own file, also in /etc/syslog.conf:

```
!ipmon
*.*		-/var/log/ipfilter.log
!pg5432
*.*		-/var/log/db/pg5432.log
```


We might also add entry in newsyslog.conf(), some app can not do it by itself because of privilege constraint:

```
/var/log/db/pg5432.log	5432:5432  640  99  4096  *  ZC
```


----------

