# Log function.

**URL:** <https://community.unix.com/t/log-function/323541>\
**Category:** Shell Programming and Scripting\
**Created:** [January 30, 2013, 8:59am UTC](https://community.unix.com/t/log-function/323541 "2013-01-30T08:59:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chacko193](https://community.unix.com/letter_avatar/chacko193/32/5_5575768a8748004e209b776fc1b2916d.png) [@chacko193](https://community.unix.com/u/chacko193)\
**Post date:** [January 30, 2013, 8:59am UTC](https://community.unix.com/t/log-function/323541/1 "2013-01-30T08:59:46Z")

</div>

I have to make a log function which will take any statement and write it to a file along with the time stamp.Right now I have the following code:

```nohighlight
log()
{
 echo "`date` `hostname`: $1" >> logfile
}
 
echo "Some statement."
log "Some statement."
 

```

This way I can see "Some statement." on my console and it will be written to logfile along with timestamp and hostname.

So is there any way to make that `echo` and `log` into one statement.  
This seems to solve the issue:

```nohighlight
log()
{
 echo "$1"
 echo "`date` `hostname`: $1" >> logfile
}
 
log "Some statement."
 

```

Is there any other way?

The thing is I want "Some statement" displayed on console without timestamp and written to a file along with the timestamp.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [January 30, 2013, 9:59am UTC](https://community.unix.com/t/log-function/323541/2 "2013-01-30T09:59:35Z")

</div>

I think you would have to show us some sample of what you want, it is still confused in my mind...

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [January 30, 2013, 10:04am UTC](https://community.unix.com/t/log-function/323541/3 "2013-01-30T10:04:38Z")

</div>

Since you want two different outputs for console & log file then you do not have any other option other than using two `echo` statements.

But if your requirement is just one output, then have a look at `tee` command manual.

---

<div class="post-metadata">

**Author:** ![chacko193](https://community.unix.com/letter_avatar/chacko193/32/5_5575768a8748004e209b776fc1b2916d.png) [@chacko193](https://community.unix.com/u/chacko193)\
**Post date:** [January 31, 2013, 1:29am UTC](https://community.unix.com/t/log-function/323541/4 "2013-01-31T01:29:45Z")

</div>

> [@vbe](#):
>
> I think you would have to show us some sample of what you want, it is still confused in my mind...

Its simple.Let's say we have some function `log()` .So when I do this:

```plaintext
log "Called log."

```

I want `Called log.` displyed on console and I want a log file in which the function makes an entry like this:

```plaintext
Wed Jan 30 23:25:02 MST 2013 : Called log.

```

I already have a code for `log()` function.

```plaintext
log()
{
 echo "$1"
 echo "`date` `hostname`: $1" >> logfile
}
 
log "Some statement."

```

My question is can I have a `log()` function without using two seperate `echo` statements?

---------- Post updated at 01:29 AM ---------- Previous update was at 01:27 AM ----------

> [@bipinajith](#):
>
> Since you want two different outputs for console & log file then you do not have any other option other than using two `echo` statements.
> 
> But if your requirement is just one output, then have a look at `tee` command manual.

But I do want a timestamp in logfile, and displaying timestamp on console kinda feels bad.

---

<div class="post-metadata">

**Author:** ![zazzybob](https://community.unix.com/user_avatar/community.unix.com/zazzybob/32/112_2.png) [@zazzybob](https://community.unix.com/u/zazzybob)\
**Post date:** [January 31, 2013, 1:53am UTC](https://community.unix.com/t/log-function/323541/5 "2013-01-31T01:53:54Z")

</div>

You can - read `man tee` as bipinajith suggests.

Then you can do things like

```nohighlight
echo "this will be displayed on my console, and will be appended to the log" | tee -a $somelog

```

But - as you want two different pieces of output (i.e. the console output does NOT contain date and hostname, but the log output does) you will need two separate statements, and tee will not work for you.

Cheers,  
ZB

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [January 31, 2013, 2:02am UTC](https://community.unix.com/t/log-function/323541/6 "2013-01-31T02:02:11Z")

</div>

put the echo inside your function

```nohighlight
log()
{
          echo "$1" 
          echo "`date` `hostname`: $1" >> logfile
} 
 
log "Some statement."

```

---

<div class="post-metadata">

**Author:** ![chacko193](https://community.unix.com/letter_avatar/chacko193/32/5_5575768a8748004e209b776fc1b2916d.png) [@chacko193](https://community.unix.com/u/chacko193)\
**Post date:** [January 31, 2013, 2:02am UTC](https://community.unix.com/t/log-function/323541/7 "2013-01-31T02:02:19Z")

</div>

> [@zazzybob](#):
>
> You can - read `man tee` as bipinajith suggests.
> 
> Then you can do things like
> 
> ```plaintext
> echo "this will be displayed on my console, and will be appended to the log" | tee -a $somelog
> 
> ```
> 
> But - as you want two different pieces of output (i.e. the console output does NOT contain date and hostname, but the log output does) you will need two separate statements, and tee will not work for you.
> 
> Cheers,  
> ZB

I'm already using ` tee -a ` .But it doesn't help me in this case.So I think its better to use two dihhrent statements,I guess.☹

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [January 31, 2013, 3:38am UTC](https://community.unix.com/t/log-function/323541/8 "2013-01-31T03:38:02Z")

</div>

```nohighlight
$ echo "`date` `hostname`: $1" | tee -a logfile | awk '{$1=$2=$3="";$0=$0;$1=$1}1'

```
