I know sails.log("some text") replaces console.log in SailsJS, but - How do I log things like info, warn and just normal text instead of error in Sails 0.10x?
This information can all be found here: http://sailsjs.org/#/documentation/concepts/Logging
Essentially you can do sails.log(), sails.warn(), sails.error, etc. and then specify which level of logs you want to see via the config/log.js file.
Use sails.log['logLevel'] like sails.log.warn or sails.log.error
Logs by order or precedence:
sails.log.error()
sails.log.warn()
sails.log.debug()
sails.log.info()
sails.log.verbose()
sails.log.silly()
by default sails log level config is sails.config.log.level = 'info' then verbose and silly dont are displayed
More infos: http://sailsjs.org/#/documentation/concepts/Logging
Related
Is there a way to add a timestamp to when my bot logs something to the console?
For example, when it leaves a server, it says:
I have been removed from the guild: "Server"
And I want it to look something like [Day, Time]:
[May 10, 2022, 15:35] I have been removed from the guild: "Server"
You Can use log-timestamp npm package to do so!
steps:
type npm i log-timestamp in console.
paste it in your main index.js file!
require('log-timestamp')
all set! now you will get timestamp in every logging in console!
You can get Date and normalize it with simple module moment.js.
Install it on terminal with npm i moment.
const moment = require("moment")
moment.locale("en")
client.on("guildDelete", async guild => {
console.log(`[${moment(Date.now()).format("LLL")}] I have been removed from the guild: ${guild.name}`)
})
Use a custom log function (e.g. client.log()) instead of console.log(). This allows you to modify logs without changing global prototype (bad practice!)
Example of my TS discord bot's custom logger
It is thrown around as log on every function. Note this code is a little advanced, so it may confuse newer readers.
I have a requirement now, when log4js is printing logs, I need to get the current real-time log data, a complete piece of log information (log4js processed).Does log4js has such an interface?
logger.error("Cheese is too ripe!");
When the code is executed,I get
[2020-07-15T11:19:07.452] [ERROR] cheese - Cheese is too ripe!
How can I get this whole string from log4js in my code rather than 'Cheese is too ripe'
I just find a way to solve it by use recording appender.
Use replay and layout method to make the same log sentence.
I am developing an express project which will have multiple modules/services in it. The folder structure looks mostly like this:
-- app.js
-- payment_service
-- routes.js
-- index.js
-- models
-- model_1.js
-- model_2.js
APIs in index.js are the only exposed APIs and they work as a gateway for all requests coming for this module/service.
Most of the services can throw operational error under many circumstances, so manual intervention may needed to fix things. So I need to:
Log errors properly with proper context so that some person/script can do the needful.
Figure out the reason of failure.
There will be dedicated teams owning each service. So I should be able to differentiate between error logs for each service so that it can be aggregated and forwarded to concerned person.
I decided to go with ELK stash so that I can generate reports by script.
The main problem that I am facing is that I can't maintain correlation between logs. For example; If a request comes and it travels through five functions and each function logs something then I can't relate those logs.
One way is to create a child logger for each request and pass it to all the functions but that seems to be extra overhead passing logger instance to all the functions.
Another option is to use something like verror and do the logging only at entry point of the service/module so that the whole context can be contained in the log. This approach looks ok for logging errors, however it can't help with info and debug logs - they help me a lot in development and testing phase.
For the sake of differentiating between error logs, I am going to create
A dedicated logger for each service with log level error.
An application wide generic logger for info and debug purpose.
Is this the correct approach?
What will be the best way so that I can achieve all the requirements in simplest way?
I'd recommend you use a logger and you don't need anything too complex. For example:
npm install 12factor-log
Then create a file in your root folder near app.js (or in a /lib folder is where I'd place libraries)
logger.js
const Log = require('12factor-log');
module.exports = (params) => {
return new Log(params);
}
Then in your modules, import your logger and pass in the module name when you instantiate it so you can track where statements come from...
model_1.js
var log = require('./logger')({name: 'model_1'});
// ...
log.info("Something happened here");
// ...
try {
// ...
catch (error) {
const message = `Error doing x, y, z with value ${val}`;
log.error(message);
throw new Error(message);
}
Then handle error gracefully at your controller -> view layer for user-friendly experience.
Your logs would print something like this:
{"ts":"2018-04-27T16:37:24.914Z","msg":"Something happened here","name":"model_1","type":"info","level":3,"hostname":"localhost","pid":18}
As far as correlation of logs, if you see in the output above it includes the hostname of the machine it's running on, and also the name of the module and severity level. You can import this JSON into Logstash and load into Elasticsearch and it will store JSON for easy search and indexing.
See: https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html
Logging is complex and many people have worked on it. I would suggest not doing so yourself.
So, not following my own advice, I created my own logging package:
https://www.npmjs.com/package/woveon-logger
npm install woveon-logger
This prints file and line numbers of errors and messages, has logging levels and aspect-oriented logging, and can dump a stack trace in one call. It even has color coding options. If you get stuck and need some feature in logging, let me know.
let logger1 = new Logger('log1', {level : 'info', debug : true, showname : true};
let logger2 = new Logger('log2', {level : 'verbose', debug : true, showname : true};
...
log1.info('Here is a log message, that is on line 23.');
log1.verbose('Does not show');
log2.verbose('Shows because log2 is verbose logging');
log2.setAspect('IO', true);
log2.aspect('IO', 'Showing aspect IO logging, for logs for IO related operations');
[2018-06-10T10:43:20.692Z] [INFO--] [log1 ] [path/to/myfile:23] Here is a log message, that is on line 23.
[2018-06-10T10:43:20.792Z] [VERBOS] [log2 ] [path/to/myfile:25] Shows because log2 is verbose logging
[2018-06-10T10:43:20.892Z] [IO----] [log2 ] [path/to/myfile:27] Showing aspect IO logging, for logs for IO related operations
Also, some other features like:
log1.throwError('Logs this as both a line of logging, and throws the error with the same message');
log1.printStack('Prints this label next to the stack trace.');
Hope it helps!
You can use grackle_tracking library https://www.getgrackle.com/analytics_and_tracking
It logs errors & traffic to your db.
Is there a way by which I can turn on/off certain logs, based on its type/levels.
For eg:
I have defined 3 levels: ALL, WARNING, CRITICAL
And I have my Log class, where I will set this. Say I set Level: 'ALL'
So this will log everything, wherever I have logged messages.
Now, when I set Level: 'WARNING'
This will only log messages, which are of warning type.
Can I do this with Bunyan ?
Or any other module?
Please help !!
One work around would be to use Bunyan's DTrace facilities.Keep the log level higher and if you need to inspect low level log like debug you can run Dtrace command
Examples
Trace all log messages coming from any Bunyan module on the system
dtrace -x strsize=4k -qn 'bunyan*:::log-*{printf("%d: %s: %s", pid, probefunc, copyinstr(arg0))}'
Trace all log messages coming from the "wuzzle" component:
dtrace -x strsize=4k -qn 'bunyan*:::log-*/strstr(this->str =
copyinstr(arg0), "\"component\":\"wuzzle\"") != NULL/{printf("%s",
this->str)}'
you need to manually install the "dtrace-provider" lib separately via npm install dtrace-provider
Check out the documentation here
try using winston module for logging .this is good for logging and has log rotation and other features
Ok I've tried searching all over but can't seem to get just a simple straight forward answer.
I want to write log messages (INFO, ERROR, etc.) to the Kohana log file /application/logs/YYYY/MM/DD.php.
How do I do it?
Try the log class add() method: http://kohanaframework.org/3.2/guide/api/Log#add
Call it like this:
Log::instance()->add(Log::NOTICE, 'My Logged Message Here');
For the first parameter (level) use one of the 9 constants defined in the log class
Shuadoc you shouldn't touch system files (all those under system folder).
Change the value in bootstrap.php instead as stated by Ygam
Otherwise, when updates come you'll be in trouble.