How do I add timestamps to bot logs? - node.js

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.

Related

Text in Bash terminal getting overwritten! Using JS, Node.js (npms are: inquirer, console.table, and mysql)

Short 10sec video of what is happening: https://drive.google.com/file/d/1YZccegry36sZIPxTawGjaQ4Sexw5zGpZ/view
I have a CLI app that asks a user for a selection, then returns a response from a mysql database. The CLI app is run in node.js and prompts questions with Inquirer.
However, after returning the table of information, the next prompt overwrites the table data, making it mostly unreadable. It should appear on its own lines beneath the rest of the data, not overlap. The functions that gather and return the data are asynchronous (they must be in order to loop), but I have tried it with just a short list of standard synchronous functions for testing purposes, and the same problem exists. I have tried it with and without console.table, and the prompt still overwrites the response, as a console table or object list.
I have enabled checkwinsize in Bash with
shopt -s checkwinsize
And it still persists.
Is it Bash? Is it a problem with Inquirer?
I was having this issue as well. In my .then method of my prompt I was using switch and case to determine what to console log depending on the users selection. Then at the bottom I had an if statement checking to if they selected 'Finish' if they didn't I called the prompt function again, which I named 'questionUser'.
That's where I was having the issue, calling questionUser again was overriding my console logs.
What I did was I wrapped the questionUser call in a setTimeout like this:
if(data.option !== 'Finish'){
setTimeout(() => {
questionUser();
}, 1000);
}
This worked for me and allowed my console log data to not get deleted.
Hopefully if anyone else is having this issue this helps, I couldn't find an answer to this specific question anywhere.

Is there a way to check if a function is missing keyword await in nodejs?

So I am using async/await in my nodejs code, and I have to use it. There is no change in that, but since I have so many files, sometimes I miss adding await when calling the async function. I tried googling to find if there is an extension to check the missing await or if there is a npm properties to check that. I cant find anyone. I found this online:
Disallow async functions which have no await expression (require-await)
https://eslint.org/docs/rules/require-await
It says require-await might help, but I am not sure how to install require-await. I tried "npm install require-await", that did not work since it says its not a valid command.
or Is there any other way to check that?

React FacebookAuth Error: FB.login() called before FB.init()

I'm using the admin-on-rest npm package starter project and trying to plug in a simple SSO Facebook login button using the FacebookAuth npm package. Every time I try to click the "Login" button, I get the following error:
FB.login() called before FB.init()
I'm using an .env file with the following variable: REACT_APP_FACEBOOK_APP_ID and setting it to the right value. I even did console.log() within my app and can see it output.
I checked and I'm only loading the FB SDK once, not multiple times (this was a common issue reported on other threads).
Ok, it turned out to be something pretty dumb, but something to point out nonetheless!
In my .env file, I had accidentally placed a semicolon (;) at the end of the declaration, like this:
REACT_APP_FACEBOOK_APP_ID = XXXXXXXXXXXX;
Apparently .env files do NOT like semi-colons. This was just very difficult to figure out from the error above.
So if any of you want to pull your hair out because of this issue, and you're using similar tech, check to make sure you're syntactically kosher EVERYWHERE variables are being declared.
the funny thing was i forgot to replace your-app-id with my app id:
<script>
FB.init({
appId: 'your-app-id',
autoLogAppEvents: true,
xfbml: true,
version: 'v8.0'
});
</script>

Logger for Sails JS?

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

How do I write log messages in Kohana 3.2?

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.

Resources