Help on best way to use / import a module

im making a system with tons of other modules in it and i have a logger module (basically if i wanted to make a big log file or like send it to a webhook or something or say it in chat) but i cant decide how i should import it

i could require it in every module that needs it
i could take it in as an argument in the initialization function in the modules
or i could just use _G.logger but i know that _G is bad practice so i really dont wanna do that but its easy

i discussed this a little with someone and they told me that _G would probably be the best option and no one would really care but i really just cant get myself to
requiring it in every module seems really slow and taking it in as an argument seems pretty ehhh

help me decide please. (OR GIVE ME BETTER IDEAS PLEASE IF YOU HAVE THEM)

1 Like

_G is just a table,you can just replace it with a module script that returns a table.

1 Like

_G is a sham. Reasons from similar posts:

image

i know and thats why i dont like using it

i really think replacing _G is a bad idea
and also just using _G in general
honestly im thinking about just requiring it because its not that really big of a performance hit

What exactly is your logger for? Are you logging every game activity? I’m only assuming so because you’re saying you need to require it in every script.

well not in every script but logging error output or info that i (or someone using the system) would need to know
like one example of a module is a permission system (that is basically like the spigot permission system) and im checking the datastore so if it fails i could just do like logger.error(errorcode)
i know i could just use error(“a”) but i want to output it to other things too without copy pasting code so yeah

my original idea was to just return an error code + message and let the main system deal with that garbage but i feel like that would get messy very quickly and annoying to deal with
(i mean like

return 1, "datastore failed"

)

You can use warn or print and have the logger have a connection to the output and just pick it up from there. Or a bindable event.

yeah i was thinking of a bindable event and i completely forgot to mention that lol
im pretty sure i was gonna go through with it but i forgot about it in the mess of deciding "should i use a signal library?? "

also wait you can connect to like warn and print and stuff?? without like setfenv magic?? how do you do this

thank you so much this lifts so much god damn weight off my back i dont need to make a logger anymore

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.