add configured loggin and production check

This commit is contained in:
rubikscraft
2022-03-07 16:36:40 +01:00
parent 8ebc473274
commit fd4a5c2293
7 changed files with 51 additions and 8 deletions

View File

@@ -4,6 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { PicsurConfigModule } from './config/config.module'; import { PicsurConfigModule } from './config/config.module';
import { ServeStaticConfigService } from './config/servestatic.config.service'; import { ServeStaticConfigService } from './config/servestatic.config.service';
import { TypeOrmConfigService } from './config/typeorm.config.service'; import { TypeOrmConfigService } from './config/typeorm.config.service';
import { PicsurLoggerModule } from './logger/logger.module';
import { DemoManagerModule } from './managers/demo/demomanager.module'; import { DemoManagerModule } from './managers/demo/demomanager.module';
import { AuthModule } from './routes/api/auth/auth.module'; import { AuthModule } from './routes/api/auth/auth.module';
import { PrefModule } from './routes/api/pref/pref.module'; import { PrefModule } from './routes/api/pref/pref.module';
@@ -24,6 +25,7 @@ import { ImageModule } from './routes/image/imageroute.module';
ImageModule, ImageModule,
DemoManagerModule, DemoManagerModule,
PrefModule, PrefModule,
PicsurLoggerModule,
], ],
}) })
export class AppModule {} export class AppModule {}

View File

@@ -21,17 +21,28 @@ export class HostConfigService {
} }
public isDemo() { public isDemo() {
const enabled = this.configService.get<boolean>(`${EnvPrefix}_DEMO`, false); const enabled = this.configService.get<boolean>(`${EnvPrefix}DEMO`, false);
this.logger.debug('Demo enabled: ' + enabled); this.logger.debug('Demo enabled: ' + enabled);
return enabled; return enabled;
} }
public getDemoInterval() { public getDemoInterval() {
const interval = this.configService.get<number>( const interval = this.configService.get<number>(
`${EnvPrefix}_DEMO_INTERVAL`, `${EnvPrefix}DEMO_INTERVAL`,
1000 * 60 * 5, 1000 * 60 * 5,
); );
this.logger.debug('Demo interval: ' + interval); this.logger.debug('Demo interval: ' + interval);
return interval; return interval;
} }
public isProduction() {
const enabled = this.configService.get<boolean>(
`${EnvPrefix}PRODUCTION`,
false,
);
if (enabled) {
this.logger.log('Running in production mode');
}
return enabled;
}
} }

View File

@@ -12,18 +12,18 @@ export class TypeOrmConfigService implements TypeOrmOptionsFactory {
public getTypeOrmServerOptions() { public getTypeOrmServerOptions() {
const varOptions = { const varOptions = {
host: this.configService.get<string>(`${EnvPrefix}_DB_HOST`, 'localhost'), host: this.configService.get<string>(`${EnvPrefix}DB_HOST`, 'localhost'),
port: this.configService.get<number>(`${EnvPrefix}_DB_PORT`, 5432), port: this.configService.get<number>(`${EnvPrefix}DB_PORT`, 5432),
username: this.configService.get<string>( username: this.configService.get<string>(
`${EnvPrefix}_DB_USERNAME`, `${EnvPrefix}DB_USERNAME`,
DefaultName, DefaultName,
), ),
password: this.configService.get<string>( password: this.configService.get<string>(
`${EnvPrefix}_DB_PASSWORD`, `${EnvPrefix}DB_PASSWORD`,
DefaultName, DefaultName,
), ),
database: this.configService.get<string>( database: this.configService.get<string>(
`${EnvPrefix}_DB_DATABASE`, `${EnvPrefix}DB_DATABASE`,
DefaultName, DefaultName,
), ),
}; };

View File

@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { PicsurConfigModule } from '../config/config.module';
import { PicsurLoggerService } from './logger.service';
@Module({
imports: [PicsurConfigModule],
providers: [PicsurLoggerService],
exports: [PicsurLoggerService],
})
export class PicsurLoggerModule {}

View File

@@ -0,0 +1,13 @@
import { ConsoleLogger, Injectable, Scope } from '@nestjs/common';
import { HostConfigService } from '../config/host.config.service';
@Injectable({ scope: Scope.DEFAULT })
export class PicsurLoggerService extends ConsoleLogger {
constructor(private hostService: HostConfigService) {
super();
if (hostService.isProduction()) {
super.setLogLevels(['error', 'warn', 'log']);
}
}
}

View File

@@ -9,7 +9,7 @@ import { AppModule } from './app.module';
import { HostConfigService } from './config/host.config.service'; import { HostConfigService } from './config/host.config.service';
import { MainExceptionFilter } from './layers/httpexception/httpexception.filter'; import { MainExceptionFilter } from './layers/httpexception/httpexception.filter';
import { SuccessInterceptor } from './layers/success/success.interceptor'; import { SuccessInterceptor } from './layers/success/success.interceptor';
import { PicsurLoggerService } from './logger/logger.service';
async function bootstrap() { async function bootstrap() {
const fastifyAdapter = new FastifyAdapter(); const fastifyAdapter = new FastifyAdapter();
@@ -20,6 +20,9 @@ async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>( const app = await NestFactory.create<NestFastifyApplication>(
AppModule, AppModule,
fastifyAdapter, fastifyAdapter,
{
bufferLogs: true
}
); );
app.useGlobalFilters(new MainExceptionFilter()); app.useGlobalFilters(new MainExceptionFilter());
app.useGlobalInterceptors(new SuccessInterceptor()); app.useGlobalInterceptors(new SuccessInterceptor());
@@ -30,6 +33,8 @@ async function bootstrap() {
}), }),
); );
app.useLogger(app.get(PicsurLoggerService));
const hostConfigService = app.get(HostConfigService); const hostConfigService = app.get(HostConfigService);
await app.listen(hostConfigService.getPort(), hostConfigService.getHost()); await app.listen(hostConfigService.getPort(), hostConfigService.getHost());
} }

View File

@@ -3,6 +3,8 @@ FROM node:16-alpine
# Sorry for the humongous docker container this generates # Sorry for the humongous docker container this generates
# Maybe I'll trim it down some day # Maybe I'll trim it down some day
ENV PICSUR_PRODUCTION=true
ADD . /picsur ADD . /picsur
WORKDIR /picsur WORKDIR /picsur