bash-oo-framework icon indicating copy to clipboard operation
bash-oo-framework copied to clipboard

[question] Logging to File, Syslog or Email

Open nicutor opened this issue 7 years ago • 4 comments

Hi,

I would like to start using this framework instead of log4sh and I want to kindly ask you if there is any example or if somebody can guide me on how to log to a file, syslog or send the logs thru email?

Thank you.

nicutor avatar Sep 25 '18 12:09 nicutor

Hello, @nicutor Based on my codereading, I think what you have to do is just to redirect stderr to file you want. so,

$ ./your-script.sh 2>log-file.txt

Because all log methods(defined in lib/util/logs.sh) seems to cat all messages to stderr

my test success(test.sh is example code at README.md, Using Basic Logging, Colors and Powerline Emoji):

<X_X>:bash-oo-framework$ ./test.sh
I'm blue...
[DEBUG] [test.sh:19] Play me some Jazz, will ya? 🎷
Something bad happened.
This will be printed to STDERR, no matter what.
0is0[feature/japanese-docs]
<X_X>:bash-oo-framework$ ./test.sh 2>log.txt
I'm blue...
0is0[feature/japanese-docs]
<X_X>:bash-oo-framework$ cat log.txt
[DEBUG] [test.sh:19] Play me some Jazz, will ya? 🎷
Something bad happened.
This will be printed to STDERR, no matter what.

++ I'm not a member of maintainer, not good at this framework, so this might be wrong. Please remember this is just my opinion...

I'm happy if this helps you.

Cj-bc avatar Sep 25 '18 13:09 Cj-bc

@Cj-bc Thank you for your help, but I am trying to avoid that way.

Based on documentation, I've already find a way, but, if there is any better example of how to log to file, syslog or email, please let me know.

#!/usr/bin/env bash
source "$( cd "${BASH_SOURCE[0]%/*}" && pwd )/lib/oo-bootstrap.sh"

import util/log

namespace myApp
myLoggingDelegate() {
    echo "$(date +'%F %T %Z') [${subject}]: $*"
    echo "$(date +'%F %T %Z') [${subject}]: $*" >> test.log
}
Log::RegisterLogger MYLOGGER myLoggingDelegate
Log::AddOutput myApp MYLOGGER

subject="WARNING" Log "Something"

nicutor avatar Sep 25 '18 14:09 nicutor

@nicutor oh, I see. I think the way you showed(declaring Logger) is only the way to do that. If I find any solution, I'll let you know.

Cj-bc avatar Sep 25 '18 21:09 Cj-bc

@nicutor's answer is correct. In the current version logging is a bit over-engineered. I'd like to simplify it in the next version that we're brainstorming in #45, feel free to join.

niieani avatar Sep 26 '18 13:09 niieani