Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is called regardless of the log level being read already or later after page load.

in other words, if this.context.level is changed at a later stage, it won't have an affect on the already created logger instance, will it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will, as the object is not deep copied but passed by reference so the logger and the logger builder share the same context (until the site it loaded).

I pushed two additional commits providing test cases for this scenario.
(I hope this is ok, normally I would open a new PR for a new feature but I think the tests belong to this)

}

Expand Down
Loading