Skip to content

Commit

Permalink
Merge pull request #202 from github-community-projects/sutterj/log-fix
Browse files Browse the repository at this point in the history
fix: add min level to logger
  • Loading branch information
sutterj authored Jul 8, 2024
2 parents cdb56da + 0d7d976 commit d071c5c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ALLOWED_ORGS=
WEBHOOK_SECRET=bad-secret

# Use `trace` to get verbose logging or `info` to show less
LOG_LEVEL=debug
LOGGING_LEVEL=debug

# Used for settings various configuration in the app
NODE_ENV=development
Expand Down
4 changes: 2 additions & 2 deletions env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const env = createEnv({
PRIVATE_KEY: z.string(),

// Optional environment variables
LOG_LEVEL: z.string().optional().default('debug'),
LOGGING_LEVEL: z.string().optional().default('debug'),
NODE_ENV: z.string().optional().default('development'),
PUBLIC_ORG: z.string().optional(),
PRIVATE_ORG: z.string().optional(),
Expand Down Expand Up @@ -60,7 +60,7 @@ export const env = createEnv({
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
WEBHOOK_SECRET: process.env.WEBHOOK_SECRET,
PRIVATE_KEY: process.env.PRIVATE_KEY,
LOG_LEVEL: process.env.LOG_LEVEL,
LOGGING_LEVEL: process.env.LOGGING_LEVEL,
NODE_ENV: process.env.NODE_ENV,
PUBLIC_ORG: process.env.PUBLIC_ORG,
PRIVATE_ORG: process.env.PRIVATE_ORG,
Expand Down
14 changes: 11 additions & 3 deletions src/app/api/auth/lib/nextauth-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,23 @@ export const nextAuthOptions: AuthOptions = {
'No allowed handles or orgs specified, allowing all users.',
)

authLogger.info('User is allowed to sign in:', profile.login)

return true
}

authLogger.debug('Trying to sign in with handle:', profile.login)

// If the user is in the allowed handles list, allow sign in
if (allowedHandles.includes(profile.login)) {
authLogger.info('User is allowed to sign in:', profile.login)

return true
}

authLogger.debug(
`User "${profile.login}" is not in the allowed handles list`,
'User is not in the allowed handles list:',
profile.login,
)

authLogger.debug(
Expand All @@ -188,13 +193,16 @@ export const nextAuthOptions: AuthOptions = {
// Check if any of the user's organizations are in the allowed orgs list
if (orgs.some((org) => allowedOrgs.includes(org.login))) {
authLogger.info(
`User "${profile.login}" has an org in the allowed orgs list`,
'User has an org in the allowed orgs list:',
profile.login,
)

authLogger.info('User is allowed to sign in:', profile.login)

return true
}

authLogger.warn(`User "${profile.login}" is not allowed to sign in`)
authLogger.warn('User is not allowed to sign in:', profile.login)

return false
},
Expand Down
5 changes: 5 additions & 0 deletions src/server/repos/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ export const createMirrorHandler = async ({
await git.checkoutBranch(input.newBranchName, defaultBranch)
await git.push('origin', input.newBranchName)

reposApiLogger.info('Mirror created', {
org: newRepo.data.owner.login,
name: newRepo.data.name,
})

return {
success: true,
data: newRepo.data,
Expand Down
13 changes: 13 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@ const getLoggerType = () => {
return 'json'
}

// Map logger level name to number for tsLog
const mapLevelToMethod: { [key: string]: number } = {
silly: 0,
trace: 1,
debug: 2,
info: 3,
warn: 4,
error: 5,
fatal: 6,
}

export const logger = new Logger({
type: getLoggerType(),
minLevel:
mapLevelToMethod[process.env.LOGGING_LEVEL?.toLowerCase() || 'info'],
maskValuesRegEx: [
/"access[-._]?token":"[^"]+"/g,
/"api[-._]?key":"[^"]+"/g,
Expand Down

0 comments on commit d071c5c

Please sign in to comment.