Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Logging to File, Syslog or Email #64

Open
nicutor opened this issue Sep 25, 2018 · 4 comments
Open

[question] Logging to File, Syslog or Email #64

nicutor opened this issue Sep 25, 2018 · 4 comments

Comments

@nicutor
Copy link

nicutor commented Sep 25, 2018

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.

@Cj-bc
Copy link
Contributor

Cj-bc commented Sep 25, 2018

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.

@nicutor
Copy link
Author

nicutor commented Sep 25, 2018

@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"

@Cj-bc
Copy link
Contributor

Cj-bc commented Sep 25, 2018

@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.

@niieani
Copy link
Owner

niieani commented Sep 26, 2018

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants