R
Railway10mo ago
Scai

NixPacks (Laravel) - Display by default laravel log files

I want to listen to storage/logs and log into the console.
27 Replies
Percy
Percy10mo ago
Project ID: N/A
Scai
ScaiOP10mo ago
N/A I think you're wrong Brody about prestart.
Scai
ScaiOP10mo ago
No description
Scai
ScaiOP10mo ago
I created a file watcher for laravel log file and console log by default to see if it works.
const logsDir = path.join(srcdir, 'storage', 'logs');
(async () => {
try {
const watcher = fs.watch(logsDir);
for await (const event of watcher) {
if (event.eventType !== 'change' || !event.filename.endsWith('.log'))
continue;

const logFile = await fs.readFile(path.join(logsDir, event.filename), 'utf-8');
const regex = /^\[(?<date>.*)\]\s(?<env>\w+)\.(?<type>\w+):(?<message>.*)/gm;

for (const match of logFile.matchAll(regex)) {
const { date, env, type, message } = match.groups;
console.log(`[${date}] ${env}.${type}: ${message}`)
}
}
} catch (e) {
console.error(e);
}
})();
const logsDir = path.join(srcdir, 'storage', 'logs');
(async () => {
try {
const watcher = fs.watch(logsDir);
for await (const event of watcher) {
if (event.eventType !== 'change' || !event.filename.endsWith('.log'))
continue;

const logFile = await fs.readFile(path.join(logsDir, event.filename), 'utf-8');
const regex = /^\[(?<date>.*)\]\s(?<env>\w+)\.(?<type>\w+):(?<message>.*)/gm;

for (const match of logFile.matchAll(regex)) {
const { date, env, type, message } = match.groups;
console.log(`[${date}] ${env}.${type}: ${message}`)
}
}
} catch (e) {
console.error(e);
}
})();
Brody
Brody10mo ago
@aleks - who wrote prestart.mjs, please confirm that on its own, it has nothing to do with file watching that is JavaScript, besides that, I don't see how any of this relates to prestart.mjs in any way shape or form may I know why it's not feasible to set laravel to log to stdout / stderr (I've seen it done before) but it is feasible to write something like that?
root
root10mo ago
Prestart does absolutely nothing with file watching, it just writes some config files and fixes permissions I've been considering writing functionality to have it pull log files into stdout, though
Brody
Brody10mo ago
okay so is there some difficulties to having Laravel send logs to stdout / stderr?
root
root10mo ago
I don't know, I've never tried
Brody
Brody10mo ago
I can't imagine it being more than a few changes
Scai
ScaiOP10mo ago
From what I saw with the code sent here, it's working fine. The image is the code output, which is fired in prestart. I mean, to hook a function on prestart what is the file watching the logs
root
root10mo ago
No, prestart exits when your app starts. I might be able to implement something like this, though - what's your idea? Maybe some ESM modules that are loaded asynchronously in, say, a .nixpacks/hooks directory?
Scai
ScaiOP10mo ago
Then, why in local development isn't exiting?
Scai
ScaiOP10mo ago
No description
Scai
ScaiOP10mo ago
I mean, we can't add start hook on StartPhase, a ESM module like you said?
Brody
Brody10mo ago
1. because now the code you wrote doesn't exit 2. because there's a single & nothing your doing has anything to do with prestart.mjs the way you think it does, you are simply just running a node script along side the php app
Scai
ScaiOP10mo ago
Hmm, okay. I understand now. I don't have experience in Rust, just trying to understand from other languages knowledge the logic and making tests to see what is working and what is not.
root
root10mo ago
What that code does is it runs the prestart script and then (after the script has exited) runs php-fpm and nginx in parallel.
Brody
Brody10mo ago
& is not a good way to do that, I know that much
root
root10mo ago
What would you suggest?
Brody
Brody10mo ago
parallel or multirun
root
root10mo ago
are those npm packages?
Scai
ScaiOP10mo ago
Wouldn't running a nodejs alongside php-fpm and nginx cause a memory increase on server? ~40MB, because of the NodeJS runtime
Brody
Brody10mo ago
as aleks has stated, prestart.mjs exits what you're doing doesn't exist, so yes what you're doing will add memory usage to the service
root
root10mo ago
(the logical continuation of that statement was "if they are, then I have moral objections")
Brody
Brody10mo ago
they are most certainly not
root
root10mo ago
good, what are they and how are they better than & oh multirun does look cool
Brody
Brody10mo ago
they both can run two (or more) commands in parallel, and they will exit all commands entirely if one command exits thus allowing railway to restart the container, unlike & where it sends the command to the background where it can die and stay dead
Want results from more Discord servers?
Add your server