Merge branch 'feat/logger-options' of github.com:payloadcms/payload
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
/* eslint-disable import/no-dynamic-require */
|
||||
/* eslint-disable global-require */
|
||||
import path from 'path';
|
||||
import pino from 'pino';
|
||||
import Logger from '../utilities/logger';
|
||||
import { SanitizedConfig } from './types';
|
||||
import findConfig from './find';
|
||||
import validate from './validate';
|
||||
@@ -8,7 +10,8 @@ import babelConfig from '../babel.config';
|
||||
|
||||
const removedExtensions = ['.scss', '.css', '.svg', '.png', '.jpg', '.eot', '.ttf', '.woff', '.woff2'];
|
||||
|
||||
const loadConfig = (): SanitizedConfig => {
|
||||
const loadConfig = (logger?: pino.Logger): SanitizedConfig => {
|
||||
const localLogger = logger ?? Logger();
|
||||
const configPath = findConfig();
|
||||
|
||||
removedExtensions.forEach((ext) => {
|
||||
@@ -35,7 +38,7 @@ const loadConfig = (): SanitizedConfig => {
|
||||
|
||||
if (config.default) config = config.default;
|
||||
|
||||
const validatedConfig = validate(config);
|
||||
const validatedConfig = validate(config, localLogger);
|
||||
|
||||
return {
|
||||
...validatedConfig,
|
||||
|
||||
@@ -7,6 +7,7 @@ import SMTPConnection from 'nodemailer/lib/smtp-connection';
|
||||
import GraphQL from 'graphql';
|
||||
import { ConnectionOptions } from 'mongoose';
|
||||
import React from 'react';
|
||||
import { LoggerOptions } from 'pino';
|
||||
import { Payload } from '..';
|
||||
import { AfterErrorHook, CollectionConfig, SanitizedCollectionConfig } from '../collections/config/types';
|
||||
import { GlobalConfig, SanitizedGlobalConfig } from '../globals/config/types';
|
||||
@@ -68,6 +69,8 @@ export type InitOptions = {
|
||||
email?: EmailOptions;
|
||||
local?: boolean;
|
||||
onInit?: (payload: Payload) => void;
|
||||
/** Pino LoggerOptions */
|
||||
loggerOptions?: LoggerOptions;
|
||||
};
|
||||
|
||||
export type AccessResult = boolean | Where;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ValidationResult } from 'joi';
|
||||
import { Logger } from 'pino';
|
||||
import schema from './schema';
|
||||
import collectionSchema from '../collections/config/schema';
|
||||
import Logger from '../utilities/logger';
|
||||
import { SanitizedConfig } from './types';
|
||||
import { SanitizedCollectionConfig } from '../collections/config/types';
|
||||
import fieldSchema, { idField } from '../fields/config/schema';
|
||||
@@ -9,8 +9,6 @@ import { SanitizedGlobalConfig } from '../globals/config/types';
|
||||
import globalSchema from '../globals/config/schema';
|
||||
import { fieldAffectsData } from '../fields/config/types';
|
||||
|
||||
const logger = Logger();
|
||||
|
||||
const validateFields = (context: string, entity: SanitizedCollectionConfig | SanitizedGlobalConfig): string[] => {
|
||||
const errors: string[] = [];
|
||||
entity.fields.forEach((field) => {
|
||||
@@ -65,7 +63,7 @@ const validateGlobals = (globals: SanitizedGlobalConfig[]): string[] => {
|
||||
return errors;
|
||||
};
|
||||
|
||||
const validateSchema = (config: SanitizedConfig): SanitizedConfig => {
|
||||
const validateSchema = (config: SanitizedConfig, logger: Logger): SanitizedConfig => {
|
||||
const result = schema.validate(config, {
|
||||
abortEarly: false,
|
||||
});
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import nodemailer, { Transporter } from 'nodemailer';
|
||||
import { Logger } from 'pino';
|
||||
import { EmailOptions, EmailTransport, hasTransport, hasTransportOptions } from '../config/types';
|
||||
import { InvalidConfiguration } from '../errors';
|
||||
import mockHandler from './mockHandler';
|
||||
import Logger from '../utilities/logger';
|
||||
import { BuildEmailResult, MockEmailHandler } from './types';
|
||||
|
||||
const logger = Logger();
|
||||
|
||||
async function handleTransport(transport: Transporter, email: EmailTransport): BuildEmailResult {
|
||||
async function handleTransport(transport: Transporter, email: EmailTransport, logger: Logger): BuildEmailResult {
|
||||
try {
|
||||
await transport.verify();
|
||||
} catch (err) {
|
||||
@@ -26,7 +24,7 @@ const ensureConfigHasFrom = (emailConfig) => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleMockAccount = async (emailConfig: EmailOptions) => {
|
||||
const handleMockAccount = async (emailConfig: EmailOptions, logger: Logger) => {
|
||||
let mockAccount: MockEmailHandler;
|
||||
try {
|
||||
mockAccount = await mockHandler(emailConfig);
|
||||
@@ -38,28 +36,25 @@ const handleMockAccount = async (emailConfig: EmailOptions) => {
|
||||
logger.info(`Mock email account password: ${pass}`);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
'There was a problem setting up the mock email handler',
|
||||
err,
|
||||
);
|
||||
logger.error('There was a problem setting up the mock email handler', err);
|
||||
}
|
||||
return mockAccount;
|
||||
};
|
||||
|
||||
export default async function buildEmail(emailConfig: EmailOptions): BuildEmailResult {
|
||||
export default async function buildEmail(emailConfig: EmailOptions, logger: Logger): BuildEmailResult {
|
||||
if (hasTransport(emailConfig) && emailConfig.transport) {
|
||||
ensureConfigHasFrom(emailConfig);
|
||||
const email = { ...emailConfig };
|
||||
const { transport } : {transport: Transporter} = emailConfig;
|
||||
return handleTransport(transport, email);
|
||||
return handleTransport(transport, email, logger);
|
||||
}
|
||||
|
||||
if (hasTransportOptions(emailConfig) && emailConfig.transportOptions) {
|
||||
ensureConfigHasFrom(emailConfig);
|
||||
const email = { ...emailConfig } as EmailTransport;
|
||||
const transport = nodemailer.createTransport(emailConfig.transportOptions);
|
||||
return handleTransport(transport, email);
|
||||
return handleTransport(transport, email, logger);
|
||||
}
|
||||
|
||||
return handleMockAccount(emailConfig);
|
||||
return handleMockAccount(emailConfig, logger);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import { APIError } from '../../errors';
|
||||
import { PayloadRequest } from '../types';
|
||||
import { SanitizedConfig } from '../../config/types';
|
||||
|
||||
const logger = Logger();
|
||||
const logger = Logger('payload');
|
||||
|
||||
const testError = new APIError('test error', 503);
|
||||
|
||||
|
||||
11
src/index.ts
11
src/index.ts
@@ -1,4 +1,5 @@
|
||||
import express, { Express, Router } from 'express';
|
||||
import pino from 'pino';
|
||||
import crypto from 'crypto';
|
||||
import {
|
||||
TypeWithID,
|
||||
@@ -78,7 +79,7 @@ export class Payload {
|
||||
|
||||
globals: Globals;
|
||||
|
||||
logger = Logger();
|
||||
logger: pino.Logger;
|
||||
|
||||
express: Express
|
||||
|
||||
@@ -117,6 +118,7 @@ export class Payload {
|
||||
* @param options
|
||||
*/
|
||||
init(options: InitOptions): void {
|
||||
this.logger = Logger('payload', options.loggerOptions);
|
||||
this.logger.info('Starting Payload...');
|
||||
if (!options.secret) {
|
||||
throw new Error(
|
||||
@@ -139,7 +141,7 @@ export class Payload {
|
||||
this.mongoURL = options.mongoURL;
|
||||
this.local = options.local;
|
||||
|
||||
this.config = loadConfig();
|
||||
this.config = loadConfig(this.logger);
|
||||
|
||||
bindOperations(this);
|
||||
bindRequestHandlers(this);
|
||||
@@ -155,7 +157,7 @@ export class Payload {
|
||||
}
|
||||
|
||||
// Configure email service
|
||||
this.email = buildEmail(this.emailOptions);
|
||||
this.email = buildEmail(this.emailOptions, this.logger);
|
||||
this.sendEmail = sendEmail.bind(this);
|
||||
|
||||
// Initialize collections & globals
|
||||
@@ -165,8 +167,7 @@ export class Payload {
|
||||
|
||||
// Connect to database
|
||||
|
||||
|
||||
connectMongoose(this.mongoURL, options.mongoOptions, options.local);
|
||||
connectMongoose(this.mongoURL, options.mongoOptions, options.local, this.logger);
|
||||
|
||||
// If not initializing locally, set up HTTP routing
|
||||
if (!this.local) {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import mongoose, { ConnectOptions } from 'mongoose';
|
||||
import Logger from '../utilities/logger';
|
||||
import pino from 'pino';
|
||||
import { connection } from './testCredentials';
|
||||
|
||||
const logger = Logger();
|
||||
|
||||
const connectMongoose = async (url: string, options: ConnectOptions, local: boolean): Promise<void> => {
|
||||
const connectMongoose = async (
|
||||
url: string,
|
||||
options: ConnectOptions,
|
||||
local: boolean,
|
||||
logger: pino.Logger,
|
||||
): Promise<void> => {
|
||||
let urlToConnect = url;
|
||||
let successfulConnectionMessage = 'Connected to Mongo server successfully!';
|
||||
const connectionOptions = {
|
||||
|
||||
@@ -4,11 +4,17 @@ import memoize from 'micro-memoize';
|
||||
|
||||
export type PayloadLogger = pino.Logger;
|
||||
|
||||
export default memoize((name = 'payload') => pino({
|
||||
name,
|
||||
enabled: falsey(process.env.DISABLE_LOGGING),
|
||||
prettyPrint: {
|
||||
ignore: 'pid,hostname',
|
||||
translateTime: 'HH:MM:ss',
|
||||
},
|
||||
}) as PayloadLogger);
|
||||
export default memoize(
|
||||
(name = 'payload', options?: pino.LoggerOptions) => pino({
|
||||
name,
|
||||
enabled: falsey(process.env.DISABLE_LOGGING),
|
||||
...(options
|
||||
? { options }
|
||||
: {
|
||||
prettyPrint: {
|
||||
ignore: 'pid,hostname',
|
||||
translateTime: 'HH:MM:ss',
|
||||
},
|
||||
}),
|
||||
}) as PayloadLogger,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user