2
* Copyright (C) 2012 10gen Inc.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Affero General Public License, version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Affero General Public License for more details.
13
* You should have received a copy of the GNU Affero General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "mongo/platform/basic.h"
19
#include "mongo/base/init.h"
20
#include "mongo/bson/util/builder.h"
21
#include "mongo/db/auth/authorization_manager.h"
22
#include "mongo/db/auth/principal_set.h"
23
#include "mongo/db/client_basic.h"
24
#include "mongo/db/server_parameters.h"
25
#include "mongo/util/log.h"
30
// Server parameter controlling whether or not user ids are included in log entries.
31
MONGO_EXPORT_STARTUP_SERVER_PARAMETER(logUserIds, bool, false);
34
* Note: When appending new strings to the builder, make sure to pass false to the
35
* includeEndingNull parameter.
37
void appendServerExtraLogContext(BufBuilder& builder) {
38
ClientBasic* clientBasic = ClientBasic::getCurrent();
41
if (!clientBasic->hasAuthorizationManager())
44
PrincipalSet::NameIterator principals =
45
clientBasic->getAuthorizationManager()->getAuthenticatedPrincipalNames();
47
if (!principals.more())
50
builder.appendStr("user:", false);
51
builder.appendStr(principals.next().toString(), false);
52
while (principals.more()) {
53
builder.appendChar(',');
54
builder.appendStr(principals.next().toString(), false);
56
builder.appendChar(' ');
59
MONGO_INITIALIZER_WITH_PREREQUISITES(SetServerLogContextFunction, ())(InitializerContext*) {
63
return Logstream::registerExtraLogContextFn(appendServerExtraLogContext);