Skip to content

Commit

Permalink
Chore(2523): Improve logging, primarily on desktop (#2518)
Browse files Browse the repository at this point in the history
* Add better formatted logger to logger package

* Use new logger in state-manager

* Use new logger in backend

* First round of desktop log updates

* Ensure renderer logs also show up in browser

* Fix tests

* Remove all references to the old logger

* Add docs and clean up logger code

* More log changes and adding timers

* Replace all usages of console and the old logger, make mobile work well with luxon

* Fix android logging with polyfill

* Fix test

* Add docs to polyfill
  • Loading branch information
Isla Koenigsknecht authored May 15, 2024
1 parent 785a8ea commit 51c9648
Show file tree
Hide file tree
Showing 198 changed files with 4,984 additions and 3,200 deletions.
26 changes: 16 additions & 10 deletions packages/backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@types/crypto-js": "^4.0.2",
"@types/express": "^4.17.9",
"@types/jest": "28.1.8",
"@types/luxon": "^3.3.0",
"@types/luxon": "^3.4.2",
"@types/mock-fs": "^4.13.1",
"@types/node": "18.11.9",
"@types/node-fetch": "^2.5.11",
Expand Down Expand Up @@ -108,9 +108,8 @@
"dotenv": "8.2.0",
"events": "^3.2.0",
"express": "^4.17.1",
"fetch-retry": "^6.0.0",
"node-fetch": "^3.3.0",
"fastq": "^1.17.1",
"fetch-retry": "^6.0.0",
"get-port": "^5.1.1",
"go-ipfs": "npm:mocked-go-ipfs@0.17.0",
"http-server": "^0.12.3",
Expand All @@ -122,8 +121,9 @@
"joi": "^17.8.1",
"level": "8.0.0",
"libp2p": "0.42.2",
"luxon": "^1.11.4",
"luxon": "^3.4.4",
"multiaddr": "^10.0.1",
"node-fetch": "^3.3.0",
"orbit-db": "0.29.0",
"orbit-db-access-controllers": "^0.4.0",
"orbit-db-identity-provider": "0.5.0",
Expand Down
18 changes: 11 additions & 7 deletions packages/backend/src/backendManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { TorControl } from './nest/tor/tor-control.service'
import { torBinForPlatform, torDirForPlatform } from './nest/common/utils'
import initRnBridge from './rn-bridge'
import { INestApplicationContext } from '@nestjs/common'
import logger from './nest/common/logger'
import { OpenServices, validateOptions } from './options'
import { SOCKS_PROXY_AGENT } from './nest/const'
import { createLogger } from './nest/common/logger'

const log = logger('backendManager')
const logger = createLogger('backendManager')

const program = new Command()

Expand All @@ -33,9 +33,11 @@ program
program.parse(process.argv)
const options = program.opts()

console.log('options', options)
logger.info('options', options)

export const runBackendDesktop = async () => {
logger.info('Running backend manager desktop')

const isDev = process.env.NODE_ENV === 'development'

const webcrypto = new Crypto()
Expand Down Expand Up @@ -69,22 +71,24 @@ export const runBackendDesktop = async () => {
try {
await connectionsManager.closeAllServices()
} catch (e) {
log.error('Error occurred while closing backend services', e)
logger.error('Error occurred while closing backend services', e)
}
if (process.send) process.send('closed-services')
}
if (message === 'leaveCommunity') {
try {
await connectionsManager.leaveCommunity()
} catch (e) {
log.error('Error occurred while leaving community', e)
logger.error('Error occurred while leaving community', e)
}
if (process.send) process.send('leftCommunity')
}
})
}

export const runBackendMobile = async () => {
logger.info('Running backend manager mobile')

// Enable triggering push notifications
process.env['BACKEND'] = 'mobile'
process.env['CONNECTION_TIME'] = (new Date().getTime() / 1000).toString() // Get time in seconds
Expand Down Expand Up @@ -131,12 +135,12 @@ const platform = options.platform

if (platform === 'desktop') {
runBackendDesktop().catch(error => {
log.error('Error occurred while initializing backend', error)
logger.error('Error occurred while initializing backend', error)
throw error
})
} else if (platform === 'mobile') {
runBackendMobile().catch(async error => {
log.error('Error occurred while initializing backend', error)
logger.error('Error occurred while initializing backend', error)
// Prevent stopping process before getting output
await new Promise<void>(resolve => {
setTimeout(() => {
Expand Down
13 changes: 8 additions & 5 deletions packages/backend/src/nest/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import { StorageModule } from './storage/storage.module'
import { IpfsModule } from './ipfs/ipfs.module'
import { Level } from 'level'
import { verifyToken } from '@quiet/common'
import { createLogger } from './common/logger'

const logger = createLogger('appModule')

@Global()
@Module({
Expand All @@ -58,7 +61,7 @@ import { verifyToken } from '@quiet/common'
export class AppModule {
static forOptions(options: ConnectionsManagerTypes) {
const configOptions: ConfigOptions = { ...options, ...new ConnectionsManagerOptions() }
console.log('configOptions', configOptions)
logger.info('configOptions', configOptions)
return {
module: AppModule,
providers: [
Expand Down Expand Up @@ -105,15 +108,15 @@ export class AppModule {
io.engine.use((req, res, next) => {
const authHeader = req.headers['authorization']
if (!authHeader) {
console.error('Backend server: No authorization header')
logger.error('Backend server: No authorization header')
res.writeHead(401, 'No authorization header')
res.end()
return
}

const token = authHeader && authHeader.split(' ')[1]
if (!token) {
console.error('Backend server: No auth token')
logger.error('Backend server: No auth token')
res.writeHead(401, 'No authorization token')
res.end()
return
Expand All @@ -122,12 +125,12 @@ export class AppModule {
if (verifyToken(options.socketIOSecret, token)) {
next()
} else {
console.error('Backend server: Unauthorized')
logger.error('Backend server: Unauthorized')
res.writeHead(401, 'Unauthorized')
res.end()
}
})
console.log('ok')
logger.info('ok')
return { server, io }
},
inject: [EXPRESS_PROVIDER],
Expand Down
8 changes: 2 additions & 6 deletions packages/backend/src/nest/common/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { logger } from '@quiet/logger'
import { createQuietLogger } from '@quiet/logger'

const createLogger = (name: string) => {
return logger('backend')(name)
}

export default createLogger
export const createLogger = createQuietLogger('backend')
12 changes: 6 additions & 6 deletions packages/backend/src/nest/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import tmp from 'tmp'
import crypto, { sign } from 'crypto'
import { type PermsData } from '@quiet/types'
import { TestConfig } from '../const'
import logger from './logger'
import { Libp2pNodeParams } from '../libp2p/libp2p.types'
import { createLibp2pAddress, createLibp2pListenAddress, isDefined } from '@quiet/common'
import { Libp2pService } from '../libp2p/libp2p.service'
import { CertFieldsTypes, getReqFieldValue, loadCSR } from '@quiet/identity'
import { execFile } from 'child_process'
import { createLogger } from './logger'

const log = logger('test')
const logger = createLogger('utils')

export interface Ports {
socksPort: number
Expand All @@ -27,7 +27,7 @@ export interface Ports {
}

export function createPaths(paths: string[]) {
console.log('creating paths in fn - utils')
logger.info('creating paths in fn - utils')
for (const path of paths) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path, { recursive: true })
Expand All @@ -37,7 +37,7 @@ export function createPaths(paths: string[]) {

export function removeFilesFromDir(dirPath: string) {
if (fs.existsSync(dirPath)) {
log(`Removing ${dirPath}`)
logger.info(`Removing ${dirPath}`)
fs.rmdirSync(dirPath, { recursive: true })
}
}
Expand Down Expand Up @@ -114,12 +114,12 @@ export const getPorts = async (): Promise<Ports> => {

export class DummyIOServer extends Server {
emit(event: string, ...args: any[]): boolean {
log(`Emitting ${event} with args:`, args)
logger.info(`Emitting ${event} with args:`, args)
return true
}

close() {
log('Closing DummyIOServer')
logger.info('Closing DummyIOServer')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import { Libp2pEvents } from '../libp2p/libp2p.types'
import { sleep } from '../common/sleep'
import { createLibp2pAddress } from '@quiet/common'
import { lib } from 'crypto-js'
import { createLogger } from '../common/logger'

const logger = createLogger('connectionsManager:test')

jest.setTimeout(100_000)

Expand Down Expand Up @@ -200,7 +203,7 @@ describe('Connections manager', () => {
const peerList: string[] = []
const peersCount = 7
for (let pCount = 0; pCount < peersCount; pCount++) {
console.log('pushing peer ', pCount)
logger.info('pushing peer ', pCount)
peerList.push(
createLibp2pAddress(`${Math.random().toString(36).substring(2, 13)}.onion`, (await createPeerId()).toString())
)
Expand Down
Loading

0 comments on commit 51c9648

Please sign in to comment.