chore: make logging good

This commit is contained in:
David Germain
2024-06-08 00:37:11 +02:00
committed by benfurber
parent 1e69076757
commit cbe9727e27
8 changed files with 71 additions and 16 deletions

View File

@@ -20,7 +20,7 @@
# docker run -v ./functions/data/emulator:/seed -v ./functions:/app/functions -p 4001-4008:4001-4008 -it emulator
#
# EXPORT (while the container is running)
# docker exec -it <conatiner_name> /app/easy-export.sh
# docker exec -it <conatiner_name> /app/export.js
# docker cp <conatiner_name>:/app/dump ./whatever
#
# HOW TO DEBUG THE CONTAINER WHILE IT IS RUNNING:
@@ -71,12 +71,14 @@ RUN \
firebase setup:emulators:storage && \
firebase setup:emulators:pubsub
COPY ./firebase.json ./firebase.json
COPY ./firebase.storage.rules ./firebase.storage.rules
COPY ./firestore.indexes.json ./firestore.indexes.json
COPY ./firestore.rules ./firestore.rules
COPY ./firebase.json ./firebase.json
COPY ./firebase.storage.rules ./firebase.storage.rules
COPY ./firestore.indexes.json ./firestore.indexes.json
COPY ./firestore.rules ./firestore.rules
COPY ./functions/emulator/easy-export.sh ./easy-export.sh
COPY ./functions/emulator/export.js ./export.js
COPY ./functions/emulator/link-logs.js ./link-logs.js
# This folder needs to exist because otherwise
# the emulators error if the user did not mount
@@ -86,8 +88,9 @@ RUN mkdir /seed
# These should be the ports specified in firebase.json
EXPOSE 4001 4002 4003 4004 4005 4006 4007 4008
# Do firebase emulators:start --help for details
CMD \
./link-logs.js & \
# Do firebase emulators:start --help for details
firebase emulators:start \
--project demo-community-platform-emulated \
--only auth,functions,firestore,pubsub,storage,hosting,database \

View File

@@ -1,5 +0,0 @@
#!/bin/bash
firebase \
emulators:export \
--project demo-community-platform-emulated \
--force ./dump

14
functions/emulator/export.js Executable file
View File

@@ -0,0 +1,14 @@
#!/usr/bin/env node
const { exec } = require('child_process')
const command = 'firebase emulators:export --project demo-community-platform-emulated --force ./dump'
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`)
return
}
console.log(`stdout: ${stdout}`)
console.error(`stderr: ${stderr}`)
})

43
functions/emulator/link-logs.js Executable file
View File

@@ -0,0 +1,43 @@
#!/usr/bin/env node
// Continuously copy the log files into a common folder
// making it easier to export them.
//
// This script knows the folder structure of the container.
//
// Symbolic links did not work.
const fs = require('fs')
const isDebug = false
log('setup...')
const files = [
'database-debug.log',
'firebase-debug.log',
'firestore-debug.log',
'pubsub-debug.log',
'ui-debug.log'
]
// clear the files so the they are also cleared on the host.
files.forEach((filename) => {
fs.writeFileSync('/app/' + filename, '')
})
fs.watch('/app', {}, (event, filename) => {
log('event: ' + event)
log('filename: ' + filename)
if (files.includes(filename)) {
log('updating... ' + filename)
fs.copyFileSync('/app/' + filename, '/app/logs/' + filename)
}
})
function log(statement) {
if (isDebug) {
console.log('[LINK-LOGS] ' + statement)
}
}

1
functions/logs/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.log

0
functions/logs/.gitkeep Normal file
View File