Skip to content

Commit

Permalink
Adds settings endpoint to configure loglevel
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Apr 7, 2016
1 parent 4072734 commit 5e9ca02
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 12 deletions.
57 changes: 57 additions & 0 deletions spec/SettingsRouter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var request = require('request');

var headers = {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test'
};


describe('SettingsRouter', () => {

it('should set the loglevel', (done) => {
request.post({
headers: headers,
json: {
'logLevel': 'silly'
},
url: 'http://localhost:8378/1/settings'
}, (err, res, body) => {
request.get({
url: 'http://localhost:8378/1/settings',
headers: headers
}, (err, res, body)=> {
body = JSON.parse(body);
expect(body.logLevel).toBe('silly');
done();
});
});
});

it('should not access without masterKey', (done) => {
request.post({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'invalid'
},
json: {
'logLevel': 'silly'
},
url: 'http://localhost:8378/1/settings'
}, (err, res, body) => {
expect(body.error).not.toBeUndefined();
expect(body.error).toBe('unauthorized');
request.get({
url: 'http://localhost:8378/1/settings',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'invalid'
}
}, (err, res, body)=> {
body = JSON.parse(body);
expect(body.error).toBe('unauthorized');
done();
});
});
})

})
7 changes: 4 additions & 3 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ import { SchemasRouter } from './Routers/SchemasRouter';
import { SessionsRouter } from './Routers/SessionsRouter';
import { UserController } from './Controllers/UserController';
import { UsersRouter } from './Routers/UsersRouter';

import { SettingsRouter } from './Routers/SettingsRouter';
import ParsePushAdapter from 'parse-server-push-adapter';
import winston from 'winston';

// Mutate the Parse object to add the Cloud Code handlers
addParseCloud();

Expand Down Expand Up @@ -152,7 +152,7 @@ class ParseServer {
}

if (verbose || process.env.VERBOSE || process.env.VERBOSE_PARSE_SERVER) {
winston.level = 'silly';
configureLogger({level: 'silly'});
}

const filesControllerAdapter = loadAdapter(filesAdapter, () => {
Expand Down Expand Up @@ -247,6 +247,7 @@ class ParseServer {
new IAPValidationRouter(),
new FeaturesRouter(),
new GlobalConfigRouter(),
new SettingsRouter(),
];

if (process.env.PARSE_EXPERIMENTAL_HOOKS_ENABLED || process.env.TESTING) {
Expand Down
8 changes: 5 additions & 3 deletions src/Routers/LogsRouter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Parse } from 'parse/node';
import PromiseRouter from '../PromiseRouter';
import * as middleware from "../middlewares";
import winston from 'winston';
import { logger, configureLogger } from '../logger';

export class LogsRouter extends PromiseRouter {

mountRoutes() {
this.route('GET','/scriptlog', middleware.promiseEnforceMasterKeyAccess, this.validateRequest, (req) => {
return this.handleGET(req);
Expand All @@ -30,9 +32,9 @@ export class LogsRouter extends PromiseRouter {
const until = req.query.until;
let size = req.query.size;
if (req.query.n) {
size = req.query.n;
size = req.query.n;
}

const order = req.query.order
const level = req.query.level;
const options = {
Expand Down
26 changes: 26 additions & 0 deletions src/Routers/SettingsRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import PromiseRouter from '../PromiseRouter';
import * as middleware from "../middlewares";
import { logger, configureLogger } from '../logger';
import winston from 'winston';

export class SettingsRouter extends PromiseRouter {
mountRoutes() {
this.route('GET', '/settings', middleware.promiseEnforceMasterKeyAccess, (req) => {
return Promise.resolve({
response: {
logLevel: winston.level
}
})
});
this.route('POST','/settings', middleware.promiseEnforceMasterKeyAccess, (req) => {
let body = req.body;
let logLevel = body.logLevel;
if (logLevel) {
configureLogger({level: logLevel});
}
return Promise.resolve({
response: body
})
});
}
}
11 changes: 5 additions & 6 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {

let currentLogsFolder = LOGS_FOLDER;

function generateTransports() {
let level = process.env.VERBOSE ? 'verbose': 'info';
function generateTransports(level) {
let transports = [
new (DailyRotateFile)({
filename: 'parse-server.info',
Expand All @@ -38,7 +37,8 @@ function generateTransports() {

const logger = new winston.Logger();

export function configureLogger({logsFolder}) {
export function configureLogger({logsFolder, level = winston.level}) {
winston.level = level;
logsFolder = logsFolder || currentLogsFolder;

if (!path.isAbsolute(logsFolder)) {
Expand All @@ -50,15 +50,14 @@ export function configureLogger({logsFolder}) {
currentLogsFolder = logsFolder;

logger.configure({
transports: generateTransports()
transports: generateTransports(level)
})
}

configureLogger({logsFolder: LOGS_FOLDER});

export function addGroup(groupName) {
let level = process.env.VERBOSE ? 'verbose': 'info';

let level = winston.level;
let transports = generateTransports().concat(new (DailyRotateFile)({
filename: groupName,
dirname: currentLogsFolder,
Expand Down

0 comments on commit 5e9ca02

Please sign in to comment.