mirror of
https://github.com/unraid/api.git
synced 2025-12-31 13:39:52 -06:00
feat: properly read log level from environment
This commit is contained in:
@@ -2,10 +2,15 @@ import { config } from 'dotenv';
|
||||
|
||||
const env =
|
||||
process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'
|
||||
? config({ debug: true, path: `./.env.${process.env.NODE_ENV}`, encoding: 'utf-8' })
|
||||
? config({
|
||||
debug: true,
|
||||
path: `./.env.${process.env.NODE_ENV}`,
|
||||
encoding: 'utf-8',
|
||||
override: true,
|
||||
})
|
||||
: config({
|
||||
path: '/usr/local/unraid-api/.env',
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
|
||||
export default env;
|
||||
export default env;
|
||||
|
||||
@@ -3,7 +3,8 @@ import { version } from 'package.json';
|
||||
export const API_VERSION =
|
||||
process.env.npm_package_version ?? version ?? new Error('API_VERSION not set');
|
||||
|
||||
export const NODE_ENV = process.env.NODE_ENV as 'development' | 'test' | 'staging' | 'production' ?? 'production';
|
||||
export const NODE_ENV =
|
||||
(process.env.NODE_ENV as 'development' | 'test' | 'staging' | 'production') ?? 'production';
|
||||
export const environment = {
|
||||
IS_MAIN_PROCESS: false,
|
||||
};
|
||||
@@ -11,7 +12,9 @@ export const CHOKIDAR_USEPOLLING = process.env.CHOKIDAR_USEPOLLING === 'true';
|
||||
export const IS_DOCKER = process.env.IS_DOCKER === 'true';
|
||||
export const DEBUG = process.env.DEBUG === 'true';
|
||||
export const INTROSPECTION = process.env.INTROSPECTION === 'true';
|
||||
export const ENVIRONMENT = process.env.ENVIRONMENT as 'production' | 'staging' | 'development' ?? 'production';
|
||||
export const ENVIRONMENT = process.env.ENVIRONMENT
|
||||
? (process.env.ENVIRONMENT as 'production' | 'staging' | 'development')
|
||||
: 'production';
|
||||
export const GRAPHQL_INTROSPECTION = Boolean(INTROSPECTION ?? DEBUG ?? ENVIRONMENT !== 'production');
|
||||
export const PORT = process.env.PORT ?? '/var/run/unraid-api.sock';
|
||||
export const DRY_RUN = process.env.DRY_RUN === 'true';
|
||||
@@ -19,15 +22,13 @@ export const BYPASS_PERMISSION_CHECKS = process.env.BYPASS_PERMISSION_CHECKS ===
|
||||
export const BYPASS_CORS_CHECKS = process.env.BYPASS_CORS_CHECKS === 'true';
|
||||
export const LOG_CORS = process.env.LOG_CORS === 'true';
|
||||
export const LOG_TYPE = (process.env.LOG_TYPE as 'pretty' | 'raw') ?? 'pretty';
|
||||
export const LOG_LEVEL = process.env.LOG_LEVEL?.toUpperCase() as
|
||||
| 'TRACE'
|
||||
| 'DEBUG'
|
||||
| 'INFO'
|
||||
| 'WARN'
|
||||
| 'ERROR'
|
||||
| 'FATAL' ?? process.env.ENVIRONMENT === 'production' ? 'INFO' : 'TRACE';
|
||||
export const MOTHERSHIP_GRAPHQL_LINK =
|
||||
process.env.MOTHERSHIP_GRAPHQL_LINK ??
|
||||
(process.env.ENVIRONMENT === 'staging'
|
||||
? 'https://staging.mothership.unraid.net/ws'
|
||||
: 'https://mothership.unraid.net/ws');
|
||||
export const LOG_LEVEL = process.env.LOG_LEVEL
|
||||
? (process.env.LOG_LEVEL.toUpperCase() as 'TRACE' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'FATAL')
|
||||
: process.env.ENVIRONMENT === 'production'
|
||||
? 'INFO'
|
||||
: 'TRACE';
|
||||
export const MOTHERSHIP_GRAPHQL_LINK = process.env.MOTHERSHIP_GRAPHQL_LINK
|
||||
? process.env.MOTHERSHIP_GRAPHQL_LINK
|
||||
: ENVIRONMENT === 'staging'
|
||||
? 'https://staging.mothership.unraid.net/ws'
|
||||
: 'https://mothership.unraid.net/ws';
|
||||
|
||||
@@ -13,8 +13,6 @@ import exitHook from 'exit-hook';
|
||||
import { WebSocket } from 'ws';
|
||||
|
||||
import { logger } from '@app/core/log';
|
||||
import { setupLogRotation } from '@app/core/logrotate/setup-logrotate';
|
||||
import { setupAuthRequest } from '@app/core/sso/auth-request-setup';
|
||||
import { fileExistsSync } from '@app/core/utils/files/file-exists';
|
||||
import { environment, PORT } from '@app/environment';
|
||||
import * as envVars from '@app/environment';
|
||||
|
||||
@@ -13,10 +13,11 @@ export class LogService {
|
||||
|
||||
shouldLog(level: LogLevel): boolean {
|
||||
const logLevelsLowToHigh = levels;
|
||||
return (
|
||||
const shouldLog = (
|
||||
logLevelsLowToHigh.indexOf(level) >=
|
||||
logLevelsLowToHigh.indexOf(LOG_LEVEL.toLowerCase() as LogLevel)
|
||||
);
|
||||
return shouldLog;
|
||||
}
|
||||
|
||||
log(message: string): void {
|
||||
|
||||
@@ -8,6 +8,7 @@ import type { LogLevel } from '@app/core/log';
|
||||
import { ECOSYSTEM_PATH, PM2_PATH } from '@app/consts';
|
||||
import { levels } from '@app/core/log';
|
||||
import { LogService } from '@app/unraid-api/cli/log.service';
|
||||
import { LOG_LEVEL, NODE_ENV } from '@app/environment';
|
||||
|
||||
interface StartCommandOptions {
|
||||
'log-level'?: string;
|
||||
@@ -21,6 +22,7 @@ export class StartCommand extends CommandRunner {
|
||||
|
||||
async configurePm2(): Promise<void> {
|
||||
if (existsSync('/tmp/pm2-configured')) {
|
||||
this.logger.debug('PM2 already configured');
|
||||
return;
|
||||
}
|
||||
// Write a temp file when first started to prevent needing to run this again
|
||||
@@ -58,7 +60,6 @@ export class StartCommand extends CommandRunner {
|
||||
|
||||
async run(_: string[], options: StartCommandOptions): Promise<void> {
|
||||
this.logger.info('Starting the Unraid API');
|
||||
|
||||
await this.configurePm2();
|
||||
|
||||
const envLog = options['log-level'] ? `LOG_LEVEL=${options['log-level']}` : '';
|
||||
|
||||
Reference in New Issue
Block a user