Skip to content

Commit

Permalink
Merge pull request #384 from susnux/fix/initial_config
Browse files Browse the repository at this point in the history
Postpone log level detection until OC loaded
  • Loading branch information
ChristophWurst authored Jan 10, 2023
2 parents 82779fa + 451c087 commit dbb2412
Show file tree
Hide file tree
Showing 9 changed files with 10,190 additions and 1,904 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/node.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# This workflow is provided via the organization template repository
#
# https://github.com/nextcloud/.github
# https://docs.github.com/en/actions/learn-github-actions/sharing-workflows-with-your-organization

name: Node

on:
pull_request:
push:
branches:
- main
- master
- stable*

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest

name: node
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Read package.json node and npm engines version
uses: skjnldsv/read-package-engines-version-actions@v2
id: versions
with:
fallbackNode: '^16'
fallbackNpm: '^8'

- name: Set up node ${{ steps.versions.outputs.nodeVersion }}
uses: actions/setup-node@v3
with:
node-version: ${{ steps.versions.outputs.nodeVersion }}

- name: Set up npm ${{ steps.versions.outputs.npmVersion }}
run: npm i -g npm@"${{ steps.versions.outputs.npmVersion }}"

- name: Install dependencies & build
run: |
npm ci
npm run build
- name: Check typings
run: |
npm run check-types
- name: npm test
run: |
npm test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# @nextcloud/logger

[![Build Status](https://img.shields.io/github/actions/workflow/status/nextcloud/nextcloud-logger/node.yml?branch=master)](https://github.com/nextcloud/nextcloud-logger/actions/workflows/node.yml)
[![npm](https://img.shields.io/npm/v/@nextcloud/logger.svg)](https://www.npmjs.com/package/@nextcloud/logger)
[![Documentation](https://img.shields.io/badge/Documentation-online-brightgreen)](https://nextcloud.github.io/nextcloud-logger/)

Expand Down
7 changes: 7 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
clearMocks: true,
preset: 'ts-jest',
collectCoverageFrom: ['lib/**/*.ts'],
testEnvironment: 'jsdom',
}
44 changes: 35 additions & 9 deletions lib/LoggerBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@
import { getCurrentUser } from '@nextcloud/auth'
import { ILogger, ILoggerFactory, LogLevel } from './contracts'

declare var OC: Nextcloud.v22.OC | Nextcloud.v23.OC | Nextcloud.v24.OC;
declare global {
interface Window {
OC: Nextcloud.v23.OC | Nextcloud.v24.OC | Nextcloud.v25.OC;
}
}

/**
* @notExported
*/
export class LoggerBuilder {

private context: any
protected context: any

private factory: ILoggerFactory
protected factory: ILoggerFactory

constructor(factory: ILoggerFactory) {
this.context = {}
this.factory = factory
// Up to, including, nextcloud 24 the loglevel was not exposed
this.context.level = (window.hasOwnProperty('OC') && OC?.config?.loglevel !== undefined) ? OC.config.loglevel : LogLevel.Warn
// Override loglevel if we are in debug mode
if (window.hasOwnProperty('OC') && OC?.debug) {
this.context.level = LogLevel.Debug
}
}

/** Set the app name within the logging context */
Expand Down Expand Up @@ -55,8 +53,36 @@ export class LoggerBuilder {
return this
}

/** Detect and use logging level configured in nextcloud config */
detectLogLevel(): LoggerBuilder {
const self = this

// Use arrow function to prevent undefined `this` within event handler
const onLoaded = () => {
if (document.readyState === "complete" || (document.readyState === "interactive" && window.OC !== undefined)) {
// Up to, including, nextcloud 24 the loglevel was not exposed
self.context.level = window.OC?.config?.loglevel !== undefined ? window.OC.config.loglevel : LogLevel.Warn
// Override loglevel if we are in debug mode
if (window.OC?.debug) {
self.context.level = LogLevel.Debug
}
document.removeEventListener("readystatechange", onLoaded)
} else {
document.addEventListener("readystatechange", onLoaded)
}
}

onLoaded()
return this
}

/** Build a logger using the logging context and factory */
build(): ILogger {
if (this.context.level === undefined) {
// No logging level set manually, use the configured one
this.detectLogLevel()
}

return this.factory(this.context)
}

Expand Down
Loading

0 comments on commit dbb2412

Please sign in to comment.