Is there something like log4j for bash script?
Is there something like log4j
for bash script? I would like to be able to treat errors differently based on their severity. For example, log4j
allows me to differentiate between errors, info, debug, etc. Is there a way I can do that for bash?
I know, I can just write a function , that write to log as the following example:
LOG () {
echo `date` $* >> /var/log/my_log.log
}
But actually I want to know if something like log4j is relevant for bash
in order to get the ability for – error level and etc
bash
add a comment |
Is there something like log4j
for bash script? I would like to be able to treat errors differently based on their severity. For example, log4j
allows me to differentiate between errors, info, debug, etc. Is there a way I can do that for bash?
I know, I can just write a function , that write to log as the following example:
LOG () {
echo `date` $* >> /var/log/my_log.log
}
But actually I want to know if something like log4j is relevant for bash
in order to get the ability for – error level and etc
bash
What does log4j do? If you mean this, which part of its feature set do you want?
– terdon♦
Jul 7 '14 at 10:06
I want to control the log sevirity as , errors , info , debug , etc , log4j enable that , and what I asked if there are something for bash
– maihabunash
Jul 7 '14 at 10:08
add a comment |
Is there something like log4j
for bash script? I would like to be able to treat errors differently based on their severity. For example, log4j
allows me to differentiate between errors, info, debug, etc. Is there a way I can do that for bash?
I know, I can just write a function , that write to log as the following example:
LOG () {
echo `date` $* >> /var/log/my_log.log
}
But actually I want to know if something like log4j is relevant for bash
in order to get the ability for – error level and etc
bash
Is there something like log4j
for bash script? I would like to be able to treat errors differently based on their severity. For example, log4j
allows me to differentiate between errors, info, debug, etc. Is there a way I can do that for bash?
I know, I can just write a function , that write to log as the following example:
LOG () {
echo `date` $* >> /var/log/my_log.log
}
But actually I want to know if something like log4j is relevant for bash
in order to get the ability for – error level and etc
bash
bash
edited 14 mins ago
codeforester
387318
387318
asked Jul 7 '14 at 9:55
maihabunashmaihabunash
2,480144767
2,480144767
What does log4j do? If you mean this, which part of its feature set do you want?
– terdon♦
Jul 7 '14 at 10:06
I want to control the log sevirity as , errors , info , debug , etc , log4j enable that , and what I asked if there are something for bash
– maihabunash
Jul 7 '14 at 10:08
add a comment |
What does log4j do? If you mean this, which part of its feature set do you want?
– terdon♦
Jul 7 '14 at 10:06
I want to control the log sevirity as , errors , info , debug , etc , log4j enable that , and what I asked if there are something for bash
– maihabunash
Jul 7 '14 at 10:08
What does log4j do? If you mean this, which part of its feature set do you want?
– terdon♦
Jul 7 '14 at 10:06
What does log4j do? If you mean this, which part of its feature set do you want?
– terdon♦
Jul 7 '14 at 10:06
I want to control the log sevirity as , errors , info , debug , etc , log4j enable that , and what I asked if there are something for bash
– maihabunash
Jul 7 '14 at 10:08
I want to control the log sevirity as , errors , info , debug , etc , log4j enable that , and what I asked if there are something for bash
– maihabunash
Jul 7 '14 at 10:08
add a comment |
3 Answers
3
active
oldest
votes
Most systems have the logger
utility, which knows how to talk to syslogd
. It allows you to set log level (severity), facility name, specify the log file to write to, send to syslogd
on a remote host, write messages to STDERR
as well as to the system log.
The logging semantics are not quite the same as those provided by tools like log4j
, but by combining the facility.level
settings with message tags, you can achieve something very close.
Examples
NOTE: These examples use the FreeBSD version of logger
. Your system may have different options, so read your local documentation!
logger -p local3.info -f /var/log/messages -t MY_LOG_TAG "something interesting happened"
This will send the message to be logged in /var/log/messages
with a severity of info
, in the local3
facility. It includes a tag (-t MY_LOG_TAG
), which is included in each line. Tags are useful for extracting log entries with grep
, awk
, etc.
logger -h loghost -p mail.crit -s -f /var/log/mail "an unrecoverable error has occurred"
This one sends the message with severity crit
in the mail
facility to the remote machine loghost
, to be logged in /var/log/mail
. The -s
causes the message to be printed on the the script's STDERR
as well as sending it to be logged.
can you please provide example how to write to some log by logger command ,
– maihabunash
Jul 7 '14 at 10:16
Does this have anything to do with bash errors? As far as I know,logger
communicates withsyslog
which has nothing to do with bash errors.
– terdon♦
Jul 7 '14 at 11:39
@terdon - I interpreted the question as wanting a logging tool for use in bash scripts.logger
is suitable for that, but if the question is specific to logging errors frombash
itself, then no -logger
won't work, and the question needs to be updated to make it more explicit.
– D_Bye
Jul 7 '14 at 12:10
add a comment |
There's good amount of detail on logging for shell scripts via global
varaibles of shell. We can emulate the similar kind of logging in shell
script: http://www.cubicrace.com/2016/03/log-tracing-mechnism-for-shell-scripts.html
The post has details on introdducing log levels like INFO , DEBUG, ERROR.
Tracing details like script entry, script exit, function entry, function exit.
Sample Log:
add a comment |
You may want to take a look at my logging implementation on GitHub:
https://github.com/codeforester/base/blob/master/lib/stdlib.sh
It supports five log levels (ERROR, WARN, INFO, DEBUG, and VERBOSE) as well as multiple loggers.
add a comment |
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f141121%2fis-there-something-like-log4j-for-bash-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Most systems have the logger
utility, which knows how to talk to syslogd
. It allows you to set log level (severity), facility name, specify the log file to write to, send to syslogd
on a remote host, write messages to STDERR
as well as to the system log.
The logging semantics are not quite the same as those provided by tools like log4j
, but by combining the facility.level
settings with message tags, you can achieve something very close.
Examples
NOTE: These examples use the FreeBSD version of logger
. Your system may have different options, so read your local documentation!
logger -p local3.info -f /var/log/messages -t MY_LOG_TAG "something interesting happened"
This will send the message to be logged in /var/log/messages
with a severity of info
, in the local3
facility. It includes a tag (-t MY_LOG_TAG
), which is included in each line. Tags are useful for extracting log entries with grep
, awk
, etc.
logger -h loghost -p mail.crit -s -f /var/log/mail "an unrecoverable error has occurred"
This one sends the message with severity crit
in the mail
facility to the remote machine loghost
, to be logged in /var/log/mail
. The -s
causes the message to be printed on the the script's STDERR
as well as sending it to be logged.
can you please provide example how to write to some log by logger command ,
– maihabunash
Jul 7 '14 at 10:16
Does this have anything to do with bash errors? As far as I know,logger
communicates withsyslog
which has nothing to do with bash errors.
– terdon♦
Jul 7 '14 at 11:39
@terdon - I interpreted the question as wanting a logging tool for use in bash scripts.logger
is suitable for that, but if the question is specific to logging errors frombash
itself, then no -logger
won't work, and the question needs to be updated to make it more explicit.
– D_Bye
Jul 7 '14 at 12:10
add a comment |
Most systems have the logger
utility, which knows how to talk to syslogd
. It allows you to set log level (severity), facility name, specify the log file to write to, send to syslogd
on a remote host, write messages to STDERR
as well as to the system log.
The logging semantics are not quite the same as those provided by tools like log4j
, but by combining the facility.level
settings with message tags, you can achieve something very close.
Examples
NOTE: These examples use the FreeBSD version of logger
. Your system may have different options, so read your local documentation!
logger -p local3.info -f /var/log/messages -t MY_LOG_TAG "something interesting happened"
This will send the message to be logged in /var/log/messages
with a severity of info
, in the local3
facility. It includes a tag (-t MY_LOG_TAG
), which is included in each line. Tags are useful for extracting log entries with grep
, awk
, etc.
logger -h loghost -p mail.crit -s -f /var/log/mail "an unrecoverable error has occurred"
This one sends the message with severity crit
in the mail
facility to the remote machine loghost
, to be logged in /var/log/mail
. The -s
causes the message to be printed on the the script's STDERR
as well as sending it to be logged.
can you please provide example how to write to some log by logger command ,
– maihabunash
Jul 7 '14 at 10:16
Does this have anything to do with bash errors? As far as I know,logger
communicates withsyslog
which has nothing to do with bash errors.
– terdon♦
Jul 7 '14 at 11:39
@terdon - I interpreted the question as wanting a logging tool for use in bash scripts.logger
is suitable for that, but if the question is specific to logging errors frombash
itself, then no -logger
won't work, and the question needs to be updated to make it more explicit.
– D_Bye
Jul 7 '14 at 12:10
add a comment |
Most systems have the logger
utility, which knows how to talk to syslogd
. It allows you to set log level (severity), facility name, specify the log file to write to, send to syslogd
on a remote host, write messages to STDERR
as well as to the system log.
The logging semantics are not quite the same as those provided by tools like log4j
, but by combining the facility.level
settings with message tags, you can achieve something very close.
Examples
NOTE: These examples use the FreeBSD version of logger
. Your system may have different options, so read your local documentation!
logger -p local3.info -f /var/log/messages -t MY_LOG_TAG "something interesting happened"
This will send the message to be logged in /var/log/messages
with a severity of info
, in the local3
facility. It includes a tag (-t MY_LOG_TAG
), which is included in each line. Tags are useful for extracting log entries with grep
, awk
, etc.
logger -h loghost -p mail.crit -s -f /var/log/mail "an unrecoverable error has occurred"
This one sends the message with severity crit
in the mail
facility to the remote machine loghost
, to be logged in /var/log/mail
. The -s
causes the message to be printed on the the script's STDERR
as well as sending it to be logged.
Most systems have the logger
utility, which knows how to talk to syslogd
. It allows you to set log level (severity), facility name, specify the log file to write to, send to syslogd
on a remote host, write messages to STDERR
as well as to the system log.
The logging semantics are not quite the same as those provided by tools like log4j
, but by combining the facility.level
settings with message tags, you can achieve something very close.
Examples
NOTE: These examples use the FreeBSD version of logger
. Your system may have different options, so read your local documentation!
logger -p local3.info -f /var/log/messages -t MY_LOG_TAG "something interesting happened"
This will send the message to be logged in /var/log/messages
with a severity of info
, in the local3
facility. It includes a tag (-t MY_LOG_TAG
), which is included in each line. Tags are useful for extracting log entries with grep
, awk
, etc.
logger -h loghost -p mail.crit -s -f /var/log/mail "an unrecoverable error has occurred"
This one sends the message with severity crit
in the mail
facility to the remote machine loghost
, to be logged in /var/log/mail
. The -s
causes the message to be printed on the the script's STDERR
as well as sending it to be logged.
edited Jul 7 '14 at 10:36
answered Jul 7 '14 at 10:14
D_ByeD_Bye
10.7k13327
10.7k13327
can you please provide example how to write to some log by logger command ,
– maihabunash
Jul 7 '14 at 10:16
Does this have anything to do with bash errors? As far as I know,logger
communicates withsyslog
which has nothing to do with bash errors.
– terdon♦
Jul 7 '14 at 11:39
@terdon - I interpreted the question as wanting a logging tool for use in bash scripts.logger
is suitable for that, but if the question is specific to logging errors frombash
itself, then no -logger
won't work, and the question needs to be updated to make it more explicit.
– D_Bye
Jul 7 '14 at 12:10
add a comment |
can you please provide example how to write to some log by logger command ,
– maihabunash
Jul 7 '14 at 10:16
Does this have anything to do with bash errors? As far as I know,logger
communicates withsyslog
which has nothing to do with bash errors.
– terdon♦
Jul 7 '14 at 11:39
@terdon - I interpreted the question as wanting a logging tool for use in bash scripts.logger
is suitable for that, but if the question is specific to logging errors frombash
itself, then no -logger
won't work, and the question needs to be updated to make it more explicit.
– D_Bye
Jul 7 '14 at 12:10
can you please provide example how to write to some log by logger command ,
– maihabunash
Jul 7 '14 at 10:16
can you please provide example how to write to some log by logger command ,
– maihabunash
Jul 7 '14 at 10:16
Does this have anything to do with bash errors? As far as I know,
logger
communicates with syslog
which has nothing to do with bash errors.– terdon♦
Jul 7 '14 at 11:39
Does this have anything to do with bash errors? As far as I know,
logger
communicates with syslog
which has nothing to do with bash errors.– terdon♦
Jul 7 '14 at 11:39
@terdon - I interpreted the question as wanting a logging tool for use in bash scripts.
logger
is suitable for that, but if the question is specific to logging errors from bash
itself, then no - logger
won't work, and the question needs to be updated to make it more explicit.– D_Bye
Jul 7 '14 at 12:10
@terdon - I interpreted the question as wanting a logging tool for use in bash scripts.
logger
is suitable for that, but if the question is specific to logging errors from bash
itself, then no - logger
won't work, and the question needs to be updated to make it more explicit.– D_Bye
Jul 7 '14 at 12:10
add a comment |
There's good amount of detail on logging for shell scripts via global
varaibles of shell. We can emulate the similar kind of logging in shell
script: http://www.cubicrace.com/2016/03/log-tracing-mechnism-for-shell-scripts.html
The post has details on introdducing log levels like INFO , DEBUG, ERROR.
Tracing details like script entry, script exit, function entry, function exit.
Sample Log:
add a comment |
There's good amount of detail on logging for shell scripts via global
varaibles of shell. We can emulate the similar kind of logging in shell
script: http://www.cubicrace.com/2016/03/log-tracing-mechnism-for-shell-scripts.html
The post has details on introdducing log levels like INFO , DEBUG, ERROR.
Tracing details like script entry, script exit, function entry, function exit.
Sample Log:
add a comment |
There's good amount of detail on logging for shell scripts via global
varaibles of shell. We can emulate the similar kind of logging in shell
script: http://www.cubicrace.com/2016/03/log-tracing-mechnism-for-shell-scripts.html
The post has details on introdducing log levels like INFO , DEBUG, ERROR.
Tracing details like script entry, script exit, function entry, function exit.
Sample Log:
There's good amount of detail on logging for shell scripts via global
varaibles of shell. We can emulate the similar kind of logging in shell
script: http://www.cubicrace.com/2016/03/log-tracing-mechnism-for-shell-scripts.html
The post has details on introdducing log levels like INFO , DEBUG, ERROR.
Tracing details like script entry, script exit, function entry, function exit.
Sample Log:
edited Oct 2 '17 at 1:48
answered Mar 3 '16 at 5:02
Piyush ChordiaPiyush Chordia
1114
1114
add a comment |
add a comment |
You may want to take a look at my logging implementation on GitHub:
https://github.com/codeforester/base/blob/master/lib/stdlib.sh
It supports five log levels (ERROR, WARN, INFO, DEBUG, and VERBOSE) as well as multiple loggers.
add a comment |
You may want to take a look at my logging implementation on GitHub:
https://github.com/codeforester/base/blob/master/lib/stdlib.sh
It supports five log levels (ERROR, WARN, INFO, DEBUG, and VERBOSE) as well as multiple loggers.
add a comment |
You may want to take a look at my logging implementation on GitHub:
https://github.com/codeforester/base/blob/master/lib/stdlib.sh
It supports five log levels (ERROR, WARN, INFO, DEBUG, and VERBOSE) as well as multiple loggers.
You may want to take a look at my logging implementation on GitHub:
https://github.com/codeforester/base/blob/master/lib/stdlib.sh
It supports five log levels (ERROR, WARN, INFO, DEBUG, and VERBOSE) as well as multiple loggers.
answered 1 hour ago
codeforestercodeforester
387318
387318
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f141121%2fis-there-something-like-log4j-for-bash-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What does log4j do? If you mean this, which part of its feature set do you want?
– terdon♦
Jul 7 '14 at 10:06
I want to control the log sevirity as , errors , info , debug , etc , log4j enable that , and what I asked if there are something for bash
– maihabunash
Jul 7 '14 at 10:08