Search

printk and console log level.

printk is used in kernel programming to print messages into the kernel logs.

The syntax of printk is



The log levels decide the importance of the message being printed, kernel defines 8 log levels in the file printk.h



We can see each log level corresponds to a number and the lower the number higher the importance of the message.
The levels are useful in deciding what should be displayed to the user on the console and what should not be.

Every console has log level called as the the console log level and any message with a log level number lesser than the console log level gets displayed on the console, and other messages which have a log level number higher or equal to the console log level are logged in the kernel log which can be looked into using the command "dmesg".

The console loglevel can be found by looking into the file /proc/sys/kernel/printk



The first number in the output is the console log level, the second is the default log level, third is the minimum log level and fourth is the maximum log level.

Log level 4 corresponds to KERN_WARNING. Thus all the messages with log levels 3,2,1 and 0 will get displayed on the screen as well as logged and the messages with log level 4,5,6,7 only get logged and can be viewed using "dmesg".

The console log level can be changed by writing into the proc entry



Now the console log level is set to 6, which is KERN_INFO.

We can test logging by using the following module

hello.c:



The printk called in the init function uses KERN_WARNING which is log level and lesser than 6 which is the console log level and hence should be seen on the screen.

The printk used in the exit function is KERN_INFO which is log level 6,same as the console log level, and hence should not be visible on the screen.

Note: We can test the operation of the code only by logging into a text mode as none of the messages are displayed on a terminal of GUI.

Makefile:



Compile and insert



We can see the hello world being printed on the screen.



The good bye world message gets logged but is not printed on the screen but can be see in the logs.

Thus using printk and the console log levels we can control the kernel messages visible to the user.


14 comments:

  1. Good Post and its informative one. Thank you for sharing this good article, it was so good to read and very useful to update my skill as updated one.

    Linux Training in Electronic City

    ReplyDelete
  2. I have seen your blog and really amazing information put in this. If anyone needs Website Designing and Digital Marketing Services in India, visit Ogen Infosystem in Delhi to get a creative and responsive website.
    Best Website Designing Company in India

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Lime Kiln Dust in Miami serves as a valuable resource for soil improvement and construction projects. With its high calcium content and binding properties, LKD enhances soil stability, regulates pH levels, and contributes to sustainable development in the Miami area.

    ReplyDelete
  5. In Riyadh, the search for the best chocolate in riyadh. Their exquisite creations boast a perfect balance of flavors, making every bite a decadent experience that delights chocolate enthusiasts in the city.

    ReplyDelete
  6. "Nonfiction Text Features" is a comprehensive guide that enhances readers' comprehension and engagement by systematically exploring headings, captions, and diagrams in factual content. It provides clear examples and explanations, promoting effective learning in educational settings and casual reading, transforming the way readers interact with nonfiction texts. Requisitos de Divorcio de Nueva York

    ReplyDelete
  7. Western Wear by Future Style By Nida Faysal epitomizes refined elegance and contemporary fashion, blending effortlessly chic designs with high-quality craftsmanship. Elevate your wardrobe with pieces that exude sophistication and style, perfect for every occasion.

    ReplyDelete
  8. "Monster Rabbit" offers premium rabbit honey UAE, sourced from the finest ingredients for enhanced intimacy and vitality. Elevate your experience with our natural and potent formula, trusted by discerning consumers for its quality and effectiveness.

    ReplyDelete
  9. Understanding log levels is crucial for effective debugging and troubleshooting. While you delve into kernel logs with "dmesg," why not unwind and relax at a massage and spa Karachi DHA to rejuvenate?

    ReplyDelete