difference between console log level and default log lovel - linux

in module programming i read ,
if log level is
less than console log level will get displayed and
higher than will be mentioned in log files ,
if i dont specify any log level in printk statement then the default log level will be taken .
i just saw the default and console log level
by
cat /proc/sys/kernel/printk
and the result was
4 4 1 7
here both default and console was same .
i dont understand why default log level created . are we going to use the default log level in anywhere .
what is the exact difference between console log level and default log level .
i am new to module programming.

As we know we have different kernel level logs:
#define KERN_EMERG "<0>" /* system is unusable*/
#define KERN_ALERT "<1>" /* action must be taken immediately*/
#define KERN_CRIT "<2>" /* critical conditions*/
#define KERN_ERR "<3>" /* error conditions*/
#define KERN_WARNING "<4>" /* warning conditions*/
#define KERN_NOTICE "<5>" /* normal but significant condition*/
#define KERN_INFO "<6>" /* informational*/
#define KERN_DEBUG "<7>" /* debug-level messages*/
Okay let us put for discussion separately:
Console log level is something which is used to set log levels that can be displayed on console window with Log levels(printk) < Console log level(4 taken considering wrt your case).
i.e., it will print kernel messages with printk using log levels from 0,1,2 and 3. rest 4 to 7 will be logged in circular buffer maintained by kernel - can be seen issuing "dmesg".
Now if we move on to Default log level:
Whenever you use printk without any log level info, say for eg:
printk("Insmod my first driver\n"); // this log level will be set to "kern_warning"(as default log level is 4).
So the difference is console log is used to decide what is needed to be printed on console and default log level is used for what log level is to be taken by default if not mentioned by printk during kernel module programming.

Related

QFileSystemWatcher file changed signal emits only ones for few file update

I am using QFileSystemWatcher to control the log file changes.
For creating and updating the log file I am using boost library.
When I log few messages in one method file changing signal emits only ones (for last message), but I see that file updating every time after log message added.
So, the code for QFileSystemWatcher is
std::string fn = "app.log";
logging::init_log(fn);
QFileSystemWatcher* watcher = new QFileSystemWatcher();
auto success = QObject::connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(handleFileChanged(QString)));
Q_ASSERT(success);
watcher->addPath(QString::fromStdString(fn));
adding log messages
void a(){
/* some code */
logging::write_log("test error", logging::kError);
logging::write_log("test info", logging::kInfo);
}
QFileSystemWatcher emits signal only ones for Info level message.
In file manager I see that file updating after each call (test error, test info).
In log file initialization I use
sink->locked_backend()->auto_flush(true);
so the file updates immediately.
How can I fix this? Or maybe there is another approach how to handle log file updating to show message in GUI.
Similar filesystem event notifications are usually collapsed into one, unless they are consumed by a reader. For example, if the writer writes 10 bytes to a file, the thread that monitors that file for writes will typically see one event instead of 10. This is explicitly outlined in inotify description notes on Linux, which is likely used internally by QFileSystemWatcher.
This should not matter for any correct implementation of a filesystem monitoring software. The notification only allows the monitor to notice that some event happened (e.g. a write occurred), and it is up to the software to discover further details about the event (e.g. the amount of data that was written, and writing position).
If you aim to just display the written logs, you should be able to just read the file contents from the current reading position to the end of the file. That read operation may return one log record or more. It can return an incomplete log record, too, if the C++ standard library is implemented in a certain way (e.g. when auto_flush is disabled, and the stream buffer fills the internal buffer with part of the log record content before issuing write). The monitoring software should parse the read content to separate log records and detect incomplete log records (e.g. split data by newline characters).

printk() messages not appearing in console

So I'm trying to learn to write Linux modules and right now I'm experimenting with a basic "Hello World" module:
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void){
printk(KERN_ALERT "Hello, world.\n");
return 0;
}
static void hello_exit(void){
printk(KERN_ALERT "goodbye.\n");
}
module_init(hello_init);
module_exit(hello_exit);
And I've finally gotten this module to work! When I add with insmod it prints "hello" to kernel.log and when I remove it with remmod it prints "goodbye" to kernel.log.
My trouble is that I decided I want to try and get the output to also print to the console. From what I understand about printk(), is that in order for messages to show up in the console, the console must be set to the appropriate message level in /proc/sys/kernel/printk. (This is all according to https://elinux.org/Debugging_by_printing). My console is set to level 4.
cat /proc/sys/kernel/printk:
4 4 1 7
Since KERN_ALERT is level 2 and my console is set to print out level 4 and below messages, why are the printk messages not appearing on my console? When I run dmesg I can see the messages are clearly in the buffer, but never go to the console. It's not I really need them to print to the console, but I really want to understand how this all works.
I hope i can answer to your question. I also faced same issue and tried my level best to print kernel message to console, but nothing works. Then I started searching for the reason...
The reason is, If klogd is not running, the message won’t reach user space unless you read /proc/kmsg. Reference: oreilly or /dev/kmsg. klogd reads kernel log messages and helps process and send those messages to the appropriate files, sockets or users. since absence of daemon, it won't send to standard output. Unless you read the message from ring buffer or buffer overflow, it will remains.

Application Insights logging with log4js in java

Azure application insights log messages using log4j framework in java as shown below.
https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-trace-logs
Is there any similar thing for nodejs application without using using azure node sdk to log messages. I am looking for logging log messages using log4js messages to App insights with some configuration changes.
If you just need to log messages. Your can use log package for nodejs.
Installation:
npm i log
This is Universal logging utility
Configurable, environment and presentation agnostic, with log levels and namespacing (debug style) suppor
Usage
Writing logs
// Default logger writes at 'info' level
const log = require("log");
// Log 'info' level message:
log("some info message %s", "injected string");
// Get namespaced logger (debug lib style)
log = log.get("my-lib");
// Log 'info' level message in context of 'my-lib' namespace:
log("some info message in 'my-lib' namespace context");
// Namespaces can be nested
log = log.get("func");
// Log 'info' level message in context of 'my-lib:func' namespace:
log("some info message in 'my-lib:func' namespace context");
// Log 'error' level message in context of 'my-lib:func' namespace:
log.error("some error message");
// log output can be dynamically enabled/disabled during runtime
const { restore } = log.error.disable();
log.error("error message not really logged");
// Restore previous logs visibiity state
restore();
log.error("error message to be logged");
Available log levels
Mirror of applicable syslog levels (in severity order):
debug - debugging information (hidden by default)
info - a purely informational message (hidden by default)
notice - condition normal, but significant
warning (also aliased as warn) - condition warning
error - condition error - to notify of errors accompanied with recovery mechanism (hence reported as log and not as uncaught exception)
Note: critical, alert, emergency are not exposed as seem to not serve a use case in context of JS applications, such errors should be exposed as typical exceptions
Output message formatting
log doesn't force any specific arguments handling. Still it is recommended to assume printf-like message format, as all currently available writers are setup to support it. Placeholders support reflects one implemented in Node.js format util
Excerpt from Node.js documentation:
The first argument is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the converted value from the corresponding argument. Supported placeholders are:
%s - String.
%d - Number (integer or floating point value).
%i - Integer.
%f - Floating point value.
%j - JSON. Replaced with the string '[Circular]' if the argument contains circular references.
%o - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, depth: 4, showProxy: true }. This will show the full object including non-enumerable symbols and properties.
%O - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable symbols and properties.
%% - single percent sign ('%'). This does not consume an argument.
Note to log writer configuration developers: For cross-env compatibility it is advised to base implementation on sprintf-kit
Enabling log writing
log on its own doesn't write anything to the console or any other means (it just emits events to be consumed by preloaded log writers).
To have logs written, the pre-chosen log writer needs to be initialized in the main (starting) module of a process.
List of available log writers
log-node - For typical Node.js processes
log-aws-lambda - For AWS lambda environment
Note: if some writer is missing, propose a PR
Logs Visibility
Default visibility depends on the enviroment (see chosen log writer for more information), and in most cases is setup through the following environment variables:
LOG_LEVEL
(defaults to notice) Lowest log level from which (upwards) all logs will be exposed.
LOG_DEBUG
Eventual list of namespaces to expose at levels below LOG_LEVEL threshold
List is comma separated as e.g. foo,-foo:bar (expose all foo but not foo:bar).
It follows convention configured within debug. To ease eventual migration from debug, configuration fallbacks to DEBUG env var if LOG_DEBUG is not present.
Timestamps logging
Writers are recommended to to expose timestamps aside each log when following env var is set
LOG_TIME
rel (default) - Logs time elapsed since logger initialization
abs - Logs absolute time in ISO 8601 format
Tests
$ npm test
Project cross-browser compatibility supported by:

Printk behaviour change with early_printk enabled

Normally printk does not print any messages before console_init which is present in start_kernel. But with early_printk enabled, printk starts printing messages before console initialization. Now how does this behaviour of printk change since I am still using printk function to print debug messages and not early_printk function. How is this mapping done?
It's not really a mapping. When early_printk is enabled, the same printk() is used as before, just new boot console is being registered in that case, and printk() uses it on early boot stages.
Look at arch/arm/kernel/early_printk.c. You can see that:
new console being registered with register_console() function
that console has CON_BOOT flag, so it's unregistered automatically as soon as real console is registered
printing happens via early_write() function, which in turn uses printch() function, which implemented for each platform separately
Where in kernel source the early_console is disabled after kernel console initialization?
It's done in register_console() function:
if (bcon &&
((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
!keep_bootcon) {
/* We need to iterate through all boot consoles, to make
* sure we print everything out, before we unregister them.
*/
for_each_console(bcon)
if (bcon->flags & CON_BOOT)
unregister_console(bcon);
}
All boot consoles are disabled by unregister_console() function in code above (when real console is being registered).
And where is the real console getting registered?
Real consoles use the same method for registration -- register_console() function. For example:
from my board's defconfig file (arch/arm/configs/omap2plus_defconfig) I can see that my board is using CONFIG_SERIAL_8250 as real console
we can search where register_console() is executed in my serial driver; it's done in univ8250_console_init() function
Is there any way to keep boot consoles up after console initialization and disable real console?
Boot consoles are automatically unregistered only when real console is registered. Following this logic, you just need to disable the real console in order to keep boot console intact.
So what you need to do, is to find out which exactly driver is used for real console in your case. You can do that looking into your .config file, or the *_defconfig file for your board. Once you located it, just disable that driver in configuration and rebuild the kernel.
If after doing so you keep observing the registering of some real console, you need to add some debug printings to register_console(), to figure out what driver is being registered, and then disable it in your configuration.

Node JS - Turn on/off logs based on type or levels

Is there a way by which I can turn on/off certain logs, based on its type/levels.
For eg:
I have defined 3 levels: ALL, WARNING, CRITICAL
And I have my Log class, where I will set this. Say I set Level: 'ALL'
So this will log everything, wherever I have logged messages.
Now, when I set Level: 'WARNING'
This will only log messages, which are of warning type.
Can I do this with Bunyan ?
Or any other module?
Please help !!
One work around would be to use Bunyan's DTrace facilities.Keep the log level higher and if you need to inspect low level log like debug you can run Dtrace command
Examples
Trace all log messages coming from any Bunyan module on the system
dtrace -x strsize=4k -qn 'bunyan*:::log-*{printf("%d: %s: %s", pid, probefunc, copyinstr(arg0))}'
Trace all log messages coming from the "wuzzle" component:
dtrace -x strsize=4k -qn 'bunyan*:::log-*/strstr(this->str =
copyinstr(arg0), "\"component\":\"wuzzle\"") != NULL/{printf("%s",
this->str)}'
you need to manually install the "dtrace-provider" lib separately via npm install dtrace-provider
Check out the documentation here
try using winston module for logging .this is good for logging and has log rotation and other features

Resources